|
1 | | -# Copyright (C) 2025 Alexander Vanhee, tfuxu |
| 1 | +# Copyright (C) 2025 Alexander Vanhee, tfuxu, kjozsa |
2 | 2 | # |
3 | 3 | # This program is free software: you can redistribute it and/or modify |
4 | 4 | # it under the terms of the GNU General Public License as published by |
@@ -47,6 +47,7 @@ def __init__(self, **kwargs): |
47 | 47 | self.current_tool_config: Optional[ToolConfig] = None |
48 | 48 | self.current_tool_option: Optional[ToolOption] = None |
49 | 49 | self._updating_ui = False |
| 50 | + self._scroll_accum = 0.0 |
50 | 51 |
|
51 | 52 | self.connect("realize", self._on_realize) |
52 | 53 |
|
@@ -95,23 +96,38 @@ def _update_ui_for_tool(self, tool_config: ToolConfig, tool_option: ToolOption): |
95 | 96 | self._updating_ui = False |
96 | 97 |
|
97 | 98 | def _on_scroll(self, controller, dx, dy): |
| 99 | + if self.current_tool_option is None: |
| 100 | + return Gdk.EVENT_PROPAGATE |
| 101 | + |
98 | 102 | modifiers = controller.get_current_event_state() |
99 | | - if (modifiers & Gdk.ModifierType.SHIFT_MASK) and (modifiers & Gdk.ModifierType.CONTROL_MASK): |
| 103 | + is_zoomed = self.get_root().image_bin.get_zoom_level() != 1 |
| 104 | + |
| 105 | + should_adjust_size = ( |
| 106 | + (modifiers & Gdk.ModifierType.SHIFT_MASK) and (modifiers & Gdk.ModifierType.CONTROL_MASK) |
| 107 | + ) or not is_zoomed |
| 108 | + |
| 109 | + if should_adjust_size: |
100 | 110 | adjustment = self.size_scale.get_adjustment() |
101 | | - min_value = adjustment.get_lower() |
102 | | - max_value = adjustment.get_upper() |
| 111 | + min_value = int(adjustment.get_lower()) |
| 112 | + max_value = int(adjustment.get_upper()) |
103 | 113 |
|
104 | | - step = math.copysign(1, -dy) if -dy != 0 else 0 |
105 | | - if dy < 0: |
106 | | - new_size = self.current_tool_option.size + step |
107 | | - else: |
108 | | - new_size = self.current_tool_option.size + step |
| 114 | + if dy == 0: |
| 115 | + return Gdk.EVENT_PROPAGATE |
109 | 116 |
|
110 | | - new_size = max(min_value, min(max_value, new_size)) |
| 117 | + step_unit = 0.35 |
| 118 | + self._scroll_accum += (-step_unit if dy > 0 else step_unit) |
| 119 | + delta = int(self._scroll_accum) |
| 120 | + |
| 121 | + if delta != 0: |
| 122 | + self._scroll_accum -= delta |
| 123 | + new_size = self.current_tool_option.size + delta |
| 124 | + new_size = max(min_value, min(max_value, new_size)) |
| 125 | + |
| 126 | + if new_size != self.current_tool_option.size: |
| 127 | + self.current_tool_option.size = new_size |
| 128 | + self.size_scale.set_value(new_size) |
| 129 | + self.trigger_action() |
111 | 130 |
|
112 | | - self.current_tool_option.size = new_size |
113 | | - self.size_scale.set_value(new_size) |
114 | | - self.trigger_action() |
115 | 131 | return Gdk.EVENT_STOP |
116 | 132 |
|
117 | 133 | return Gdk.EVENT_PROPAGATE |
@@ -192,6 +208,3 @@ def trigger_action(self): |
192 | 208 | data_json = self.current_tool_option.serialize() |
193 | 209 | param = GLib.Variant('s', data_json) |
194 | 210 | action.activate(param) |
195 | | - |
196 | | - |
197 | | - |
|
0 commit comments