Skip to content

Commit

Permalink
Use stateful actions for sorting order
Browse files Browse the repository at this point in the history
  • Loading branch information
diegogangl committed Sep 15, 2024
1 parent 9a07dc8 commit ff566ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
37 changes: 21 additions & 16 deletions GTG/gtk/browser/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,6 @@ def _set_actions(self):
('recurring_month', self.on_set_recurring_every_month, None),
('recurring_year', self.on_set_recurring_every_year, None),
('recurring_toggle', self.on_toggle_recurring, None),
('sort_asc', self.on_sort_order_asc, None),
('sort_desc', self.on_sort_order_desc, None),
]

for action, callback, accel in action_entries:
Expand All @@ -268,6 +266,14 @@ def _set_actions(self):
sort_action.connect('change-state', self.on_sort)
self.add_action(sort_action)

order_variant = GLib.Variant.new_string('ASC')
order_action = Gio.SimpleAction.new_stateful('sort_order',
order_variant.get_type(),
order_variant)

order_action.connect('change-state', self.on_sort_order)
self.add_action(order_action)


def _init_icon_theme(self):
"""
Expand Down Expand Up @@ -1305,6 +1311,19 @@ def on_sort(self, action, value) -> None:
self.store_sorting(value_str.lower())


def on_sort_order(self, action, value) -> None:

action.set_state(value)
value_str = value.get_string()

if value_str == 'ASC':
self.get_pane().set_sort_order(reverse=False)
self.change_sort_icon('ASC')
else:
self.get_pane().set_sort_order(reverse=True)
self.change_sort_icon('DESC')


def store_sorting(self, mode: str) -> None:
"""Store sorting mode."""

Expand All @@ -1317,20 +1336,6 @@ def store_sorting(self, mode: str) -> None:
self.config.set('sort_mode_open', mode)


def on_sort_order_asc(self, action, params) -> None:
"""Set ascending order for current sorter."""

self.get_pane().set_sort_order(reverse=False)
self.change_sort_icon('ASC')


def on_sort_order_desc(self, action, params) -> None:
"""Set descending order for current sorter."""

self.get_pane().set_sort_order(reverse=True)
self.change_sort_icon('DESC')


def set_sorter(self, value: str) -> None:
"""Set sorter for current task pane."""

Expand Down
6 changes: 4 additions & 2 deletions GTG/gtk/data/main_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,13 @@
<section>
<item>
<attribute name="label" translatable="yes">Ascending Order</attribute>
<attribute name="action">win.sort_asc</attribute>
<attribute name="target">ASC</attribute>
<attribute name="action">win.sort_order</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Descending Order</attribute>
<attribute name="action">win.sort_desc</attribute>
<attribute name="target">DESC</attribute>
<attribute name="action">win.sort_order</attribute>
</item>
</section>
</menu>
Expand Down

0 comments on commit ff566ec

Please sign in to comment.