Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle common aspect ratios #132

Merged
merged 10 commits into from
Aug 21, 2024
2 changes: 1 addition & 1 deletion data/launcher.desktop.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Desktop Entry]
Name=Dippi
GenericName=Display information calculator
Comment=Calculate display info like DPI and aspect ratio
Comment=Calculate display info like DPI
Categories=Calculator;Utility;
Exec=com.github.cassidyjames.dippi
Icon=com.github.cassidyjames.dippi
Expand Down
12 changes: 12 additions & 0 deletions data/metainfo.appdata.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@
</screenshot>
</screenshots>
<releases>
<release version="4.1.1" date="2024-08-21">
<description>
<p>Lies! Deception!</p>
<ul>
<li>Better handle several common resolutions/aspect ratios</li>
</ul>
</description>
<issues>
<issue url="https://github.com/cassidyjames/dippi/issues/6">Common aspect ratios are dumb</issue>
<issue url="https://github.com/cassidyjames/dippi/issues/55">Unexpected ratio for 3440×1440</issue>
</issues>
</release>
<release version="4.1.0" date="2024-08-20">
<description>
<p>Lighten up!</p>
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(
'com.github.cassidyjames.dippi',
'vala', 'c',
version: '4.1.0',
version: '4.1.1',
meson_version: '>=1.3.2',
)

Expand Down
2 changes: 1 addition & 1 deletion src/App.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2018–2023 Cassidy James Blaede <[email protected]>
* SPDX-FileCopyrightText: 2018–2024 Cassidy James Blaede <[email protected]>
*/

public class Dippi.App : Adw.Application {
Expand Down
5 changes: 5 additions & 0 deletions src/Config.vala.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2018–2024 Cassidy James Blaede <[email protected]>
*/

public const string APP_ID = @APP_ID@;
public const string VERSION = @VERSION@;
21 changes: 10 additions & 11 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2018–2023 Cassidy James Blaede <[email protected]>
* SPDX-FileCopyrightText: 2018–2024 Cassidy James Blaede <[email protected]>
*/

public class Dippi.MainWindow : Adw.ApplicationWindow {
Expand All @@ -16,10 +16,7 @@ public class Dippi.MainWindow : Adw.ApplicationWindow {
private const int EXTERNAL_UNCLEAR_RANGE = 20;

private const double INCHES_INFER_EXTERNAL = 18;
private const int DPI_INFER_HIDPI = 192; // According to GNOME

private int aspect_width = DEFAULT_ASPECT_WIDTH;
private int aspect_height = DEFAULT_ASPECT_HEIGHT;
private const int DPI_INFER_HIDPI = 192; // According to GNOME and elementary

private double inches = 0.0;
private int width = 0;
Expand All @@ -38,7 +35,7 @@ public class Dippi.MainWindow : Adw.ApplicationWindow {
private Gtk.Stack range_stack;
private Utils.DisplayType display_type;

public MainWindow (Gtk.Application application) {
public MainWindow (Adw.Application application) {
Object (
application: application,
resizable: false
Expand Down Expand Up @@ -98,6 +95,7 @@ public class Dippi.MainWindow : Adw.ApplicationWindow {
pixel_size = 128
};

///TRANSLATORS: Label for the entry for the diagonal size of the display, e.g. 27-inches
var diag_label = new Gtk.Label (_("Diagonal size:")) {
halign = Gtk.Align.END
};
Expand All @@ -110,6 +108,7 @@ public class Dippi.MainWindow : Adw.ApplicationWindow {
var diag_entry_focus_controller = new Gtk.EventControllerFocus ();
diag_entry.add_controller (diag_entry_focus_controller);

///TRANSLATORS: Label for the entry for the pixel resolution of the display, e.g. 1920×1080
var res_label = new Gtk.Label (_("Resolution:")) {
halign = Gtk.Align.END
};
Expand All @@ -130,13 +129,14 @@ public class Dippi.MainWindow : Adw.ApplicationWindow {
var height_entry_focus_controller = new Gtk.EventControllerFocus ();
height_entry.add_controller (height_entry_focus_controller);

var x_label = new Gtk.Label (_("×"));
var px_label = new Gtk.Label (_("px"));
var x_label = new Gtk.Label ("×");
var px_label = new Gtk.Label ("px");

var inches_label = new Gtk.Label (_("inches")) {
halign = Gtk.Align.START
};

///TRANSLATORS: Used to label the selection for type of display (e.g. laptop or desktop)
var type_label = new Gtk.Label (_("Type:")) {
halign = Gtk.Align.END
};
Expand Down Expand Up @@ -192,6 +192,7 @@ public class Dippi.MainWindow : Adw.ApplicationWindow {

link_button = new Gtk.LinkButton.with_label (
"https://cassidyjames.com/dippi/",
///TRANSLATORS: label for the button to open a web page to share the results of the calculation
_("Share results…")
) {
halign = Gtk.Align.END,
Expand Down Expand Up @@ -429,9 +430,7 @@ public class Dippi.MainWindow : Adw.ApplicationWindow {

private void recalculate_aspect (int width, int height) {
if (width > 0 && height > 0) {
aspect_width = width / Utils.greatest_common_divisor (width, height);
aspect_height = height / Utils.greatest_common_divisor (width, height);
aspect_result_label.label = (aspect_width).to_string () + _(":") + (aspect_height).to_string ();
aspect_result_label.label = Utils.common_ratio (width, height);
} else {
aspect_result_label.label = "";
}
Expand Down
94 changes: 93 additions & 1 deletion src/Utils.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2018–2022 Cassidy James Blaede <[email protected]>
* SPDX-FileCopyrightText: 2018–2024 Cassidy James Blaede <[email protected]>
*/

namespace Dippi.Utils {
Expand All @@ -11,9 +11,11 @@ namespace Dippi.Utils {
public string to_string () {
switch (this) {
case INTERNAL:
///TRANSLATORS: label for the button to select an internal/laptop display
return _("Laptop");

case EXTERNAL:
///TRANSLATORS: label for the button to select an external/desktop monitor
return _("Desktop");

default:
Expand Down Expand Up @@ -60,4 +62,94 @@ namespace Dippi.Utils {
return greatest_common_divisor (a, b % a);
}
}

public string common_ratio (int width, int height) {
// We don't need every possible resolution/aspect ratio handled here, but
// https://en.wikipedia.org/wiki/List_of_computer_display_standards
// is a decent place to check. For now, this is just case-by-case for
// common resolutions and aspect ratios we've seen in the wild.
//
// We can also use this to catch "backwards" ratios, e.g. portrait
// displays (like on a smartphone).
//
// In general, put the true (or approximately true) ratio first, then put
// any other aspect ratio (like one used in marketing) in parenthesis.

int aspect_width = width / greatest_common_divisor (width, height);
int aspect_height = height / greatest_common_divisor (width, height);

string aspect_string = "%i:%i".printf (aspect_width, aspect_height);

switch (aspect_string) {
case "1:2":
case "2:1":
return "2:1 (18:9)";

case "2:3":
return "3:2";

case "3:4":
return "4:3";

// The funkiest
case "64:45":
case "45:64":
return "2:√2";

case "5:8":
case "8:5":
return "8:5 (16:10)";

// Some smartphones
case "18:37":
case "37:18":
return "37:18 (18.5:9)";

// Pretty much just iPhone absurdity
case "422:195":
case "195:422":
case "466:215":
case "215:466":
case "284:131":
case "131:284":
case "463:214":
case "214:463":
case "448:207":
case "207:448":
case "812:375":
case "375:812":
case "6:13":
case "13:6":
return "13:6 (19.5:9)";

case "84:187":
case "187:84":
case "101:45":
case "45:101":
return "20:9";

case "3:7":
case "7:3":
// e.g. 3440×1440
case "43:18":
return "7:3 (21:9)";

// 1360×768
case "48:85":
case "85:48":
// 1366×768
case "384:683":
case "683:384":
// iPhone 6–8
case "667:375":
case "375:667":
// iPhone 5
case "71:40":
case "40:71":
return "16:9";

default:
return aspect_string;
}
}
}