From 86fc446cd42ea94f998314578d67857c47a44c8f Mon Sep 17 00:00:00 2001 From: DJABhipHop Date: Mon, 13 Mar 2023 20:28:48 -0400 Subject: [PATCH 01/11] Make entry readonly --- src/MainWindow.vala | 1 + 1 file changed, 1 insertion(+) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 12cd0bbc..411c9e05 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -115,6 +115,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { entry = new Gtk.Entry () { xalign = 1, vexpand = true, + sensitive = false, valign = Gtk.Align.FILL }; entry.add_css_class (Granite.STYLE_CLASS_H2_LABEL); From 960384def878c1c2e63dc62d9f6c52fdea51bded Mon Sep 17 00:00:00 2001 From: DJABhipHop Date: Mon, 13 Mar 2023 20:29:00 -0400 Subject: [PATCH 02/11] Make control c & control v work with a read only entry --- src/MainWindow.vala | 58 +++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 411c9e05..01c59289 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -20,6 +20,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { private static GLib.Settings settings; + private Gdk.Clipboard clipboard; private Gtk.Revealer extended_revealer; private Gtk.Entry entry; @@ -84,6 +85,9 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { resizable = false; title = _("Calculator"); + var display = Gdk.Display.get_default (); + clipboard = display.get_clipboard (); + decimal_places = settings.get_int ("decimal-places"); eval = new Core.Evaluation (); @@ -510,47 +514,33 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { } } - private void copy () { - int start, end; - entry.get_selection_bounds (out start, out end); - var text_selected = end - start != 0; - - // we have to copy text in both cases - // because seems like application action blocks entry's action - if (!text_selected) { - entry.get_clipboard ().set_text (entry.text); - } else { - entry.get_clipboard ().set_text (entry.text.slice (start, end)); + public void copy () { + if (entry.get_text () != "") { + try { + var output = eval.evaluate (entry.get_text (), decimal_places); + clipboard.set_text (output); + } catch (Core.OUT_ERROR e) { + infobar_label.label = e.message; + infobar.revealed = true; + } } } - private void paste () { - get_clipboard_text.begin ((obj, res) => { - var text = get_clipboard_text.end (res); - if (text == null) { - return; + public void paste () { + var cancellable = new GLib.Cancellable (); + clipboard.read_text_async.begin (cancellable, (source, res) => { + try { + var output = eval.evaluate (clipboard.read_text_async.end (res), decimal_places); + if (entry.get_text () != output) { + entry.set_text (output); + } + } catch (Error e) { + infobar_label.label = e.message; + infobar.revealed = true; } - - int start, end; - entry.get_selection_bounds (out start, out end); - - var before = entry.text.slice (0, start); - var after = entry.text.slice (end, entry.text_length); - - entry.text = before + text + after; - entry.set_position (before.char_count () + text.char_count ()); }); } - private async string? get_clipboard_text () { - try { - return yield entry.get_clipboard ().read_text_async (null); - } catch (Error e) { - warning (e.message); - return null; - } - } - private void action_insert (SimpleAction action, Variant? variant) { var token = variant.get_string (); int new_position = entry.get_position (); From 9b61c1b18b94ab5b0c393a815ecf0f192cf783e9 Mon Sep 17 00:00:00 2001 From: DJABhipHop Date: Mon, 13 Mar 2023 20:52:50 -0400 Subject: [PATCH 03/11] Init Keyboard support --- src/MainWindow.vala | 212 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 174 insertions(+), 38 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 01c59289..3921a3b4 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -21,6 +21,46 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { private static GLib.Settings settings; private Gdk.Clipboard clipboard; + private Gtk.EventControllerKey event_controller; + + private Button button_0; + private Button button_1; + private Button button_2; + private Button button_3; + private Button button_4; + private Button button_5; + private Button button_6; + private Button button_7; + private Button button_8; + private Button button_9; + private Button button_add; + private Button button_sub; + private Button button_mult; + private Button button_div; + private Button button_point; + private Button button_percent; + private Button button_clr; + private Button button_par_left; + private Button button_par_right; + private Button button_pow; + private Button button_sr; + private Button button_sin; + private Button button_sinh; + private Button button_cos; + private Button button_cosh; + private Button button_tan; + private Button button_tanh; + private Button button_pi; + private Button button_e; + private Button button_log; + private Button button_ln; + private Button button_asin; + private Button button_acos; + private Button button_atan; + private Button button_reciprocal; + private Button button_m_add; + private Button button_m_sub; + private Button button_ms; private Gtk.Revealer extended_revealer; private Gtk.Entry entry; @@ -88,6 +128,9 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { var display = Gdk.Display.get_default (); clipboard = display.get_clipboard (); + event_controller = new Gtk.EventControllerKey (); + event_controller.key_pressed.connect (on_key_press); + decimal_places = settings.get_int ("decimal-places"); eval = new Core.Evaluation (); @@ -139,7 +182,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { tooltip_text = _("Backspace") }; - var button_clr = new Button ("C") { + button_clr = new Button ("C") { action_name = ACTION_PREFIX + ACTION_CLEAR }; button_clr.tooltip_markup = Granite.markup_accel_tooltip ( @@ -148,91 +191,91 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { ); button_clr.add_css_class (Granite.STYLE_CLASS_DESTRUCTIVE_ACTION); - var button_add = new Button (" + ") { + button_add = new Button (" + ") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("+"), tooltip_text = _("Add") }; button_add.add_css_class (Granite.STYLE_CLASS_H3_LABEL); - var button_sub = new Button (" − ") { + button_sub = new Button (" − ") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("-"), tooltip_text = _("Subtract") }; button_sub.add_css_class (Granite.STYLE_CLASS_H3_LABEL); - var button_mult = new Button (" × ") { + button_mult = new Button (" × ") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("×"), tooltip_text = _("Multiply") }; button_mult.add_css_class (Granite.STYLE_CLASS_H3_LABEL); - var button_div = new Button (" ÷ ") { + button_div = new Button (" ÷ ") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("÷"), tooltip_text = _("Divide") }; button_div.add_css_class (Granite.STYLE_CLASS_H3_LABEL); - var button_0 = new Button ("0") { + button_0 = new Button ("0") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("0") }; - var button_point = new Button (Posix.nl_langinfo (Posix.NLItem.RADIXCHAR)) { + button_point = new Button (Posix.nl_langinfo (Posix.NLItem.RADIXCHAR)) { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string (Posix.nl_langinfo (Posix.NLItem.RADIXCHAR)) }; - var button_percent = new Button ("%") { + button_percent = new Button ("%") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("%"), tooltip_text = _("Percentage") }; - var button_1 = new Button ("1") { + button_1 = new Button ("1") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("1") }; - var button_2 = new Button ("2") { + button_2 = new Button ("2") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("2") }; - var button_3 = new Button ("3") { + button_3 = new Button ("3") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("3") }; - var button_4 = new Button ("4") { + button_4 = new Button ("4") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("4") }; - var button_5 = new Button ("5") { + button_5 = new Button ("5") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("5") }; - var button_6 = new Button ("6") { + button_6 = new Button ("6") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("6") }; - var button_7 = new Button ("7") { + button_7 = new Button ("7") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("7") }; - var button_8 = new Button ("8") { + button_8 = new Button ("8") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("8") }; - var button_9 = new Button ("9") { + button_9 = new Button ("9") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("9") }; @@ -270,7 +313,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { basic_grid.attach (button_ans, 2, 5, 1, 1); basic_grid.attach (button_calc, 3, 5, 1, 1); - var button_ms = new Button ("MS") { + button_ms = new Button ("MS") { tooltip_text = _("Set memory value") }; @@ -279,11 +322,11 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { tooltip_text = _("Recall value from memory") }; - var button_m_add = new Button ("M+") { + button_m_add = new Button ("M+") { tooltip_text = _("Add to stored value") }; - var button_m_sub = new Button ("M−") { + button_m_sub = new Button ("M−") { tooltip_text = _("Subtract from stored value") }; @@ -297,109 +340,109 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { tooltip_text = _("Grand Total") }; - var button_par_left = new Button ("(") { + button_par_left = new Button ("(") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("("), tooltip_text = _("Start Group") }; - var button_par_right = new Button (")") { + button_par_right = new Button (")") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string (")"), tooltip_text = _("End Group") }; - var button_pow = new Button ("xy") { + button_pow = new Button ("xy") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("^"), tooltip_text = _("Exponent") }; - var button_sr = new Button ("√") { + button_sr = new Button ("√") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("√"), tooltip_text = _("Root") }; - var button_sin = new Button ("sin") { + button_sin = new Button ("sin") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("sin"), tooltip_text = _("Sine") }; - var button_sinh = new Button ("sinh") { + button_sinh = new Button ("sinh") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("sinh"), tooltip_text = _("Hyperbolic Sine") }; - var button_cos = new Button ("cos") { + button_cos = new Button ("cos") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("cos"), tooltip_text = _("Cosine") }; - var button_cosh = new Button ("cosh") { + button_cosh = new Button ("cosh") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("cosh"), tooltip_text = _("Hyperbolic Cosine") }; - var button_tan = new Button ("tan") { + button_tan = new Button ("tan") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("tan"), tooltip_text = _("Tangent") }; - var button_tanh = new Button ("tanh") { + button_tanh = new Button ("tanh") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("tanh"), tooltip_text = _("Hyperbolic Tangent") }; - var button_pi = new Button ("π") { + button_pi = new Button ("π") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("π"), tooltip_text = _("Pi") }; - var button_e = new Button ("e") { + button_e = new Button ("e") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("e"), tooltip_text = _("Euler's Number") }; - var button_log = new Button ("log10") { + button_log = new Button ("log10") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("log"), tooltip_text = _("Logarithm Base 10") }; - var button_ln = new Button ("ln") { + button_ln = new Button ("ln") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("ln"), tooltip_text = _("Natural Logarithm") }; - var button_asin = new Button ("sin-1") { + button_asin = new Button ("sin-1") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("asin"), tooltip_text = _("Inverse Sine") }; - var button_acos = new Button ("cos-1") { + button_acos = new Button ("cos-1") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("acos"), tooltip_text = _("Inverse Cosine") }; - var button_atan = new Button ("tan-1") { + button_atan = new Button ("tan-1") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("atan"), tooltip_text = _("Inverse Tangent") }; - var button_reciprocal = new Button ("x-1") { + button_reciprocal = new Button ("x-1") { tooltip_text = _("Reciprocal") }; @@ -455,6 +498,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { }; main_grid.append (basic_grid); main_grid.append (extended_revealer); + ((Gtk.Widget) main_grid).add_controller (event_controller); infobar_label = new Gtk.Label (""); @@ -838,4 +882,96 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { Signal.stop_emission_by_name ((void*) entry.get_delegate (), "insert-text"); } } + + private bool on_key_press (Gtk.EventControllerKey controller, uint keyval, uint keycode, Gdk.ModifierType mod_state) { + event_controller.forward (entry.get_delegate ()); + switch (keyval) { + case Gdk.Key.@0: + case Gdk.Key.KP_0: + button_0.activate (); + return true; + case Gdk.Key.@1: + case Gdk.Key.KP_1: + button_1.activate (); + return true; + case Gdk.Key.@2: + case Gdk.Key.KP_2: + button_2.activate (); + return true; + case Gdk.Key.@3: + case Gdk.Key.KP_3: + button_3.activate (); + return true; + case Gdk.Key.@4: + case Gdk.Key.KP_4: + button_4.activate (); + return true; + case Gdk.Key.@5: + case Gdk.Key.KP_5: + button_5.activate (); + return true; + case Gdk.Key.@6: + case Gdk.Key.KP_6: + button_6.activate (); + return true; + case Gdk.Key.@7: + case Gdk.Key.KP_7: + button_7.activate (); + return true; + case Gdk.Key.@8: + case Gdk.Key.KP_8: + button_8.activate (); + return true; + case Gdk.Key.@9: + case Gdk.Key.KP_9: + button_9.activate (); + return true; + case Gdk.Key.plus: + case Gdk.Key.KP_Add: + button_add.activate (); + return true; + case Gdk.Key.minus: + case Gdk.Key.KP_Subtract: + button_sub.activate (); + return true; + case Gdk.Key.asterisk: + case Gdk.Key.KP_Multiply: + button_mult.activate (); + return true; + case Gdk.Key.slash: + case Gdk.Key.KP_Divide: + button_div.activate (); + return true; + case Gdk.Key.period: + case Gdk.Key.decimalpoint: + case Gdk.Key.KP_Decimal: + button_point.activate (); + return true; + case Gdk.Key.BackSpace: + case Gdk.Key.KP_Delete: + button_del.activate (); + return true; + case Gdk.Key.Return: + case Gdk.Key.KP_Enter: + case Gdk.Key.KP_Equal: + button_calc.activate (); + return true; + case Gdk.Key.Escape: + button_clr.activate (); + return true; + } + + switch (keyval) { + case Gdk.Key.percent: + button_percent.activate (); + return true; + case Gdk.Key.parenleft: + button_par_left.activate (); + return true; + case Gdk.Key.parenright: + button_par_right.activate (); + return true; + } + return false; + } } From 5d2fca99a252ca2d3e7fa2efcdf42b8ec7bdafb0 Mon Sep 17 00:00:00 2001 From: DJABhipHop Date: Tue, 14 Mar 2023 08:44:13 -0400 Subject: [PATCH 04/11] Add placeholder text entry an set it to 0 --- src/MainWindow.vala | 1 + 1 file changed, 1 insertion(+) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 3921a3b4..916e7a51 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -166,6 +166,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { valign = Gtk.Align.FILL }; entry.add_css_class (Granite.STYLE_CLASS_H2_LABEL); + entry.set_placeholder_text ("0"); button_calc = new Button ("=") { tooltip_text = _("Calculate Result") From f0c9ffef200eae60408f0d30c8888079d127927d Mon Sep 17 00:00:00 2001 From: DJABhipHop Date: Tue, 14 Mar 2023 08:44:57 -0400 Subject: [PATCH 05/11] Add Gdk.EVENT_PROPAGATE --- src/MainWindow.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 916e7a51..b8cf05b5 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -973,6 +973,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { button_par_right.activate (); return true; } - return false; + return Gdk.EVENT_PROPAGATE; } } From 2d8c0e34647527ed65715f0694e8ae3e3b805270 Mon Sep 17 00:00:00 2001 From: DJABhipHop Date: Tue, 14 Mar 2023 08:44:38 -0400 Subject: [PATCH 06/11] Add event_controller to window AKA this --- src/MainWindow.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index b8cf05b5..88e0c3d8 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -499,7 +499,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { }; main_grid.append (basic_grid); main_grid.append (extended_revealer); - ((Gtk.Widget) main_grid).add_controller (event_controller); infobar_label = new Gtk.Label (""); @@ -513,6 +512,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { var global_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0); global_box.append (infobar); global_box.append (main_grid); + ((Gtk.Widget) this).add_controller (event_controller); child = global_box; set_titlebar (headerbar); From 7984eae06522e19873c366ca56b635ca908a03f6 Mon Sep 17 00:00:00 2001 From: DJABhipHop Date: Tue, 14 Mar 2023 08:57:46 -0400 Subject: [PATCH 07/11] Deactivate focus Since entry is now in read only mode .grab_focus () & etc is no longer needed; --- src/MainWindow.vala | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 88e0c3d8..e76098c3 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -517,12 +517,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { child = global_box; set_titlebar (headerbar); - entry.grab_focus (); - - entry.changed.connect (remove_error); - entry.activate.connect (button_calc_clicked); - entry.get_delegate ().insert_text.connect (replace_text); - button_calc.clicked.connect (() => {button_calc_clicked ();}); button_del.clicked.connect (() => {button_del_clicked ();}); button_ans.clicked.connect (() => {button_ans_clicked ();}); @@ -602,7 +596,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { entry.do_insert_text (token, -1, ref cursor_position); new_position += token.char_count (); - entry.grab_focus (); entry.set_position (new_position); } @@ -617,7 +610,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { entry.delete_text (selection_start, selection_end); entry.insert_text (function_call, -1, ref selection_start); new_position += function_call.char_count (); - entry.grab_focus (); entry.set_position (new_position); } else { activate_action (ACTION_INSERT, variant); @@ -649,7 +641,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { remove_error (); } - entry.grab_focus (); entry.set_position (position); } @@ -679,7 +670,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { entry.set_text (new_text); } - entry.grab_focus (); entry.set_position (position - 1); } @@ -791,10 +781,8 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { private void action_clear () { position = 0; entry.set_text (""); - set_focus (entry); remove_error (); - entry.grab_focus (); entry.set_position (position); } @@ -820,7 +808,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { extended_revealer.reveal_child = false; } /* Focusing button_calc because without a new focus it will cause weird window drawing problems. */ - entry.grab_focus (); entry.set_position (position); } @@ -854,7 +841,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { var cursor_position = entry.cursor_position; entry.do_insert_text (input, -1, ref cursor_position); position += input.length; - entry.grab_focus (); entry.set_position (position); } From 12fdb04b39bc60d59e50640f275a01b29a2b9816 Mon Sep 17 00:00:00 2001 From: DJABhipHop Date: Tue, 14 Mar 2023 09:29:53 -0400 Subject: [PATCH 08/11] Remove dead, unused or duplicate code --- src/MainWindow.vala | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index e76098c3..2a6b06e4 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -848,28 +848,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { infobar.revealed = false; } - private void replace_text (string new_text, int new_text_length, ref int position) { - var replacement_text = ""; - - switch (new_text) { - case ".": - case ",": - replacement_text = Posix.nl_langinfo (Posix.NLItem.RADIXCHAR); - break; - case "/": - replacement_text = "÷"; - break; - case "*": - replacement_text = "×"; - break; - } - - if (replacement_text != "" && replacement_text != new_text) { - entry.do_insert_text (replacement_text, entry.cursor_position + replacement_text.char_count (), ref position); - Signal.stop_emission_by_name ((void*) entry.get_delegate (), "insert-text"); - } - } - private bool on_key_press (Gtk.EventControllerKey controller, uint keyval, uint keycode, Gdk.ModifierType mod_state) { event_controller.forward (entry.get_delegate ()); switch (keyval) { From 72630b7bc50c756c7eed159145c60fc482d60431 Mon Sep 17 00:00:00 2001 From: DJABhipHop Date: Tue, 14 Mar 2023 09:31:51 -0400 Subject: [PATCH 09/11] Fix escape key Remove application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_CLEAR, {"Escape"}); & let event control handle it or the escape button will not animate. --- src/MainWindow.vala | 1 - 1 file changed, 1 deletion(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 2a6b06e4..939db0c9 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -117,7 +117,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { add_action_entries (ACTION_ENTRIES, this); var application_instance = (Gtk.Application) GLib.Application.get_default (); - application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_CLEAR, {"Escape"}); application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_UNDO, {"z"}); application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_COPY, {"c"}); application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_PASTE, {"v"}); From 61f226c2997b595c09726ad79863566f255a7e80 Mon Sep 17 00:00:00 2001 From: DJABhipHop Date: Tue, 14 Mar 2023 09:50:17 -0400 Subject: [PATCH 10/11] Swap Gdk.Key.Return Gdk.Key.equal Since Gdk.Key.Return is user by default to trigger the selected UI element when navigating the UI with the arrow keys or the tab keys the equal button will never get pressed or fire, But instead the highlight button will be pressed so Gdk.Key.equal makes the most since --- src/MainWindow.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 939db0c9..665e76c7 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -915,7 +915,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { case Gdk.Key.KP_Delete: button_del.activate (); return true; - case Gdk.Key.Return: + case Gdk.Key.equal: case Gdk.Key.KP_Enter: case Gdk.Key.KP_Equal: button_calc.activate (); From 9334b62c67d24d98468f3b0995312881d08929f6 Mon Sep 17 00:00:00 2001 From: DJABhipHop Date: Thu, 16 Mar 2023 10:07:32 -0400 Subject: [PATCH 11/11] Redo on_key_press to activate glib actions instead of the buttons clicked function - Exposing every single buttons like this isn't necessary & calling the for each button is a better idea EXP: activate_action(ACTION_INSERT, new GLib.Variant("s", "Character Gos Here")); is batter. The del calc button do not use glib actions Calling button_del_clicked(); & button_calc_clicked(); is nessary. --- src/MainWindow.vala | 166 +++++++++++++++++--------------------------- 1 file changed, 63 insertions(+), 103 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 665e76c7..fea509e2 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -23,45 +23,6 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { private Gdk.Clipboard clipboard; private Gtk.EventControllerKey event_controller; - private Button button_0; - private Button button_1; - private Button button_2; - private Button button_3; - private Button button_4; - private Button button_5; - private Button button_6; - private Button button_7; - private Button button_8; - private Button button_9; - private Button button_add; - private Button button_sub; - private Button button_mult; - private Button button_div; - private Button button_point; - private Button button_percent; - private Button button_clr; - private Button button_par_left; - private Button button_par_right; - private Button button_pow; - private Button button_sr; - private Button button_sin; - private Button button_sinh; - private Button button_cos; - private Button button_cosh; - private Button button_tan; - private Button button_tanh; - private Button button_pi; - private Button button_e; - private Button button_log; - private Button button_ln; - private Button button_asin; - private Button button_acos; - private Button button_atan; - private Button button_reciprocal; - private Button button_m_add; - private Button button_m_sub; - private Button button_ms; - private Gtk.Revealer extended_revealer; private Gtk.Entry entry; private Gtk.Button button_calc; @@ -173,7 +134,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { button_calc.add_css_class (Granite.STYLE_CLASS_H2_LABEL); button_calc.add_css_class (Granite.STYLE_CLASS_SUGGESTED_ACTION); - button_ans = new Button ("ANS") { + var button_ans = new Button ("ANS") { sensitive = false, tooltip_text = _("Insert last result") }; @@ -182,7 +143,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { tooltip_text = _("Backspace") }; - button_clr = new Button ("C") { + var button_clr = new Button ("C") { action_name = ACTION_PREFIX + ACTION_CLEAR }; button_clr.tooltip_markup = Granite.markup_accel_tooltip ( @@ -191,91 +152,91 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { ); button_clr.add_css_class (Granite.STYLE_CLASS_DESTRUCTIVE_ACTION); - button_add = new Button (" + ") { + var button_add = new Button (" + ") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("+"), tooltip_text = _("Add") }; button_add.add_css_class (Granite.STYLE_CLASS_H3_LABEL); - button_sub = new Button (" − ") { + var button_sub = new Button (" − ") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("-"), tooltip_text = _("Subtract") }; button_sub.add_css_class (Granite.STYLE_CLASS_H3_LABEL); - button_mult = new Button (" × ") { + var button_mult = new Button (" × ") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("×"), tooltip_text = _("Multiply") }; button_mult.add_css_class (Granite.STYLE_CLASS_H3_LABEL); - button_div = new Button (" ÷ ") { + var button_div = new Button (" ÷ ") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("÷"), tooltip_text = _("Divide") }; button_div.add_css_class (Granite.STYLE_CLASS_H3_LABEL); - button_0 = new Button ("0") { + var button_0 = new Button ("0") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("0") }; - button_point = new Button (Posix.nl_langinfo (Posix.NLItem.RADIXCHAR)) { + var button_point = new Button (Posix.nl_langinfo (Posix.NLItem.RADIXCHAR)) { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string (Posix.nl_langinfo (Posix.NLItem.RADIXCHAR)) }; - button_percent = new Button ("%") { + var button_percent = new Button ("%") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("%"), tooltip_text = _("Percentage") }; - button_1 = new Button ("1") { + var button_1 = new Button ("1") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("1") }; - button_2 = new Button ("2") { + var button_2 = new Button ("2") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("2") }; - button_3 = new Button ("3") { + var button_3 = new Button ("3") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("3") }; - button_4 = new Button ("4") { + var button_4 = new Button ("4") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("4") }; - button_5 = new Button ("5") { + var button_5 = new Button ("5") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("5") }; - button_6 = new Button ("6") { + var button_6 = new Button ("6") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("6") }; - button_7 = new Button ("7") { + var button_7 = new Button ("7") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("7") }; - button_8 = new Button ("8") { + var button_8 = new Button ("8") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("8") }; - button_9 = new Button ("9") { + var button_9 = new Button ("9") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("9") }; @@ -313,136 +274,136 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { basic_grid.attach (button_ans, 2, 5, 1, 1); basic_grid.attach (button_calc, 3, 5, 1, 1); - button_ms = new Button ("MS") { + var button_ms = new Button ("MS") { tooltip_text = _("Set memory value") }; - button_mr = new Button ("MR") { + var button_mr = new Button ("MR") { sensitive = false, tooltip_text = _("Recall value from memory") }; - button_m_add = new Button ("M+") { + var button_m_add = new Button ("M+") { tooltip_text = _("Add to stored value") }; - button_m_sub = new Button ("M−") { + var button_m_sub = new Button ("M−") { tooltip_text = _("Subtract from stored value") }; - button_mc = new Button ("MC") { + var button_mc = new Button ("MC") { sensitive = false, tooltip_text = _("Clear memory") }; - button_gt = new Button ("GT") { + var button_gt = new Button ("GT") { sensitive = false, tooltip_text = _("Grand Total") }; - button_par_left = new Button ("(") { + var button_par_left = new Button ("(") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("("), tooltip_text = _("Start Group") }; - button_par_right = new Button (")") { + var button_par_right = new Button (")") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string (")"), tooltip_text = _("End Group") }; - button_pow = new Button ("xy") { + var button_pow = new Button ("xy") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("^"), tooltip_text = _("Exponent") }; - button_sr = new Button ("√") { + var button_sr = new Button ("√") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("√"), tooltip_text = _("Root") }; - button_sin = new Button ("sin") { + var button_sin = new Button ("sin") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("sin"), tooltip_text = _("Sine") }; - button_sinh = new Button ("sinh") { + var button_sinh = new Button ("sinh") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("sinh"), tooltip_text = _("Hyperbolic Sine") }; - button_cos = new Button ("cos") { + var button_cos = new Button ("cos") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("cos"), tooltip_text = _("Cosine") }; - button_cosh = new Button ("cosh") { + var button_cosh = new Button ("cosh") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("cosh"), tooltip_text = _("Hyperbolic Cosine") }; - button_tan = new Button ("tan") { + var button_tan = new Button ("tan") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("tan"), tooltip_text = _("Tangent") }; - button_tanh = new Button ("tanh") { + var button_tanh = new Button ("tanh") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("tanh"), tooltip_text = _("Hyperbolic Tangent") }; - button_pi = new Button ("π") { + var button_pi = new Button ("π") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("π"), tooltip_text = _("Pi") }; - button_e = new Button ("e") { + var button_e = new Button ("e") { action_name = ACTION_PREFIX + ACTION_INSERT, action_target = new Variant.string ("e"), tooltip_text = _("Euler's Number") }; - button_log = new Button ("log10") { + var button_log = new Button ("log10") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("log"), tooltip_text = _("Logarithm Base 10") }; - button_ln = new Button ("ln") { + var button_ln = new Button ("ln") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("ln"), tooltip_text = _("Natural Logarithm") }; - button_asin = new Button ("sin-1") { + var button_asin = new Button ("sin-1") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("asin"), tooltip_text = _("Inverse Sine") }; - button_acos = new Button ("cos-1") { + var button_acos = new Button ("cos-1") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("acos"), tooltip_text = _("Inverse Cosine") }; - button_atan = new Button ("tan-1") { + var button_atan = new Button ("tan-1") { action_name = ACTION_PREFIX + ACTION_FUNCTION, action_target = new Variant.string ("atan"), tooltip_text = _("Inverse Tangent") }; - button_reciprocal = new Button ("x-1") { + var button_reciprocal = new Button ("x-1") { tooltip_text = _("Reciprocal") }; @@ -848,92 +809,91 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { } private bool on_key_press (Gtk.EventControllerKey controller, uint keyval, uint keycode, Gdk.ModifierType mod_state) { - event_controller.forward (entry.get_delegate ()); switch (keyval) { case Gdk.Key.@0: case Gdk.Key.KP_0: - button_0.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "0")); return true; case Gdk.Key.@1: case Gdk.Key.KP_1: - button_1.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "1")); return true; case Gdk.Key.@2: case Gdk.Key.KP_2: - button_2.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "2")); return true; case Gdk.Key.@3: case Gdk.Key.KP_3: - button_3.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "3")); return true; case Gdk.Key.@4: case Gdk.Key.KP_4: - button_4.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "4")); return true; case Gdk.Key.@5: case Gdk.Key.KP_5: - button_5.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "5")); return true; case Gdk.Key.@6: case Gdk.Key.KP_6: - button_6.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "6")); return true; case Gdk.Key.@7: case Gdk.Key.KP_7: - button_7.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "7")); return true; case Gdk.Key.@8: case Gdk.Key.KP_8: - button_8.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "8")); return true; case Gdk.Key.@9: case Gdk.Key.KP_9: - button_9.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "9")); return true; case Gdk.Key.plus: case Gdk.Key.KP_Add: - button_add.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "+")); return true; case Gdk.Key.minus: case Gdk.Key.KP_Subtract: - button_sub.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "-")); return true; case Gdk.Key.asterisk: case Gdk.Key.KP_Multiply: - button_mult.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "*")); return true; case Gdk.Key.slash: case Gdk.Key.KP_Divide: - button_div.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "/")); return true; case Gdk.Key.period: case Gdk.Key.decimalpoint: case Gdk.Key.KP_Decimal: - button_point.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", ".")); return true; case Gdk.Key.BackSpace: case Gdk.Key.KP_Delete: - button_del.activate (); + button_del_clicked(); return true; case Gdk.Key.equal: case Gdk.Key.KP_Enter: case Gdk.Key.KP_Equal: - button_calc.activate (); + button_calc_clicked (); return true; case Gdk.Key.Escape: - button_clr.activate (); + activate_action(ACTION_CLEAR, null); return true; } switch (keyval) { case Gdk.Key.percent: - button_percent.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "%")); return true; case Gdk.Key.parenleft: - button_par_left.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", "(")); return true; case Gdk.Key.parenright: - button_par_right.activate (); + activate_action(ACTION_INSERT, new GLib.Variant("s", ")")); return true; } return Gdk.EVENT_PROPAGATE;