Commit bd0ed63038

bd0ed6303830b48e30dd1af473724b7675878e73

parent: 84141e4680

Unsigned

siriusmart <siriusmart@tuta.io> · 2025-08-12 10:51 UTC

closed #96 custom scroll bar behaviour

Layout: unified · split

src/config/main.rs +13
@@ -30,6 +30,8 @@ pub struct MainConfig {
3030 pub syncing: SyncConfig,
3131 #[serde(default)]
3232 pub limits: Limits,
33 #[serde(default = "textbar_scroll_behaviour_default")]
34 pub textbar_scroll_behaviour: TextbarScrollBehaviour,
3335 #[serde(default = "image_index_default")]
3436 // The image to download from the array of images provided by the invidious api
3537 // 0 is usually `maxres` and 3 (default) is good enough for normal uses without having huge files sizes
@@ -65,6 +67,7 @@ impl Default for MainConfig {
6567 image_index: image_index_default(),
6668 refresh_after_modifying_search_filters: refresh_after_modifying_search_filters_default(
6769 ),
70 textbar_scroll_behaviour: textbar_scroll_behaviour_default(),
6871 limits: Limits::default(),
6972 syncing: sync_config_default(),
7073 provider: provider_default(),
@@ -300,3 +303,13 @@ const fn sync_videos_cooldown_secs_default() -> u64 {
300303const fn legacy_input_handling_default() -> bool {
301304 false
302305}
306
307#[derive(Serialize, Deserialize, Clone, Copy)]
308pub enum TextbarScrollBehaviour {
309 Character,
310 Word,
311 History,
312}
313const fn textbar_scroll_behaviour_default() -> TextbarScrollBehaviour {
314 TextbarScrollBehaviour::History
315}
src/items/searchbar.rs +58 −3
@@ -238,18 +238,73 @@ impl FrameworkItem for SearchBar {
238238
239239 fn message(
240240 &mut self,
241 _framework: &mut tui_additions::framework::FrameworkClean,
241 framework: &mut tui_additions::framework::FrameworkClean,
242242 data: std::collections::HashMap<String, Box<dyn std::any::Any>>,
243243 ) -> bool {
244244 if !data.contains_key("type") {
245245 return false;
246246 }
247247
248 let scroll_behaviour = framework
249 .data
250 .global
251 .get::<MainConfig>()
252 .unwrap()
253 .textbar_scroll_behaviour;
254
248255 data.get("type").is_some_and(|v| {
256 // copied from key_event
257 // if modify this, also modify it there
249258 v.downcast_ref::<String>()
250259 .is_some_and(|v| match v.as_str() {
251 "scrollup" => self.text_field.left().is_ok(),
252 "scrolldown" => self.text_field.right().is_ok(),
260 "scrollup" => match scroll_behaviour {
261 TextbarScrollBehaviour::Character => self.text_field.left().is_ok(),
262 TextbarScrollBehaviour::Word => {
263 previous_word(&mut self.text_field);
264 true
265 }
266 TextbarScrollBehaviour::History => {
267 let history = framework.data.global.get::<SearchHistory>().unwrap();
268 if self.history_index != Some(0) && !history.0.is_empty() {
269 if self.history_index.is_none() {
270 self.custom_value_cache = self.text_field.content.clone();
271 self.history_index = None
272 }
273 self.history_index =
274 Some(self.history_index.unwrap_or(history.0.len()) - 1);
275 self.text_field.content =
276 history.0[self.history_index.unwrap()].clone();
277 let _ = self.text_field.last();
278 true
279 } else {
280 false
281 }
282 }
283 },
284 "scrolldown" => match scroll_behaviour {
285 TextbarScrollBehaviour::Character => self.text_field.right().is_ok(),
286 TextbarScrollBehaviour::Word => {
287 next_word(&mut self.text_field);
288 true
289 }
290 TextbarScrollBehaviour::History => {
291 let history = framework.data.global.get::<SearchHistory>().unwrap();
292 if self.history_index.is_none() {
293 false
294 } else if self.history_index == Some(history.0.len() - 1) {
295 self.history_index = None;
296 self.text_field.content = self.custom_value_cache.clone();
297 let _ = self.text_field.last();
298 true
299 } else {
300 self.history_index = self.history_index.map(|n| n + 1);
301 self.text_field.content =
302 history.0[self.history_index.unwrap()].clone();
303 let _ = self.text_field.last();
304 true
305 }
306 }
307 },
253308 _ => false,
254309 })
255310 })
src/run.rs +107 −2
@@ -73,6 +73,12 @@ pub fn run(
7373 .unwrap()
7474 .mouse_support =>
7575 {
76 let scroll_behaviour = framework
77 .data
78 .global
79 .get::<MainConfig>()
80 .unwrap()
81 .textbar_scroll_behaviour;
7682 let command_capture = &mut framework
7783 .data
7884 .global
@@ -81,8 +87,55 @@ pub fn run(
8187 .command_capture;
8288
8389 match mouse.kind {
90 // copied from messagebar.rs::key_event
91 // if modify this, also modify it there
8492 MouseEventKind::ScrollUp if command_capture.is_some() => {
85 updated = updated || command_capture.as_mut().unwrap().left().is_ok()
93 updated = updated
94 || match scroll_behaviour {
95 TextbarScrollBehaviour::Character => {
96 command_capture.as_mut().unwrap().left().is_ok()
97 }
98 TextbarScrollBehaviour::History => {
99 let status = framework.data.global.get::<Status>().unwrap();
100 let history =
101 framework.data.global.get::<CommandHistory>().unwrap();
102 if status.command_history_index != Some(0)
103 && !history.0.is_empty()
104 {
105 let new_index =
106 status.command_history_index.unwrap_or(history.0.len())
107 - 1;
108 let new_content = history.0[new_index].clone();
109
110 let status =
111 framework.data.global.get_mut::<Status>().unwrap();
112 let text_field = status.command_capture.as_mut().unwrap();
113 if status.command_history_index.is_none() {
114 status.command_editing_cache =
115 text_field.content.clone();
116 }
117 text_field.content = new_content;
118 status.command_history_index = Some(new_index);
119 let _ = text_field.last();
120 true
121 } else {
122 false
123 }
124 }
125 TextbarScrollBehaviour::Word => {
126 previous_word(
127 framework
128 .data
129 .global
130 .get_mut::<Status>()
131 .unwrap()
132 .command_capture
133 .as_mut()
134 .unwrap(),
135 );
136 true
137 }
138 }
86139 }
87140 MouseEventKind::ScrollUp => {
88141 let data: Box<dyn Any> = Box::new("scrollup".to_string());
@@ -90,7 +143,59 @@ pub fn run(
90143 || framework.message(HashMap::from([("type".to_string(), data)]))
91144 }
92145 MouseEventKind::ScrollDown if command_capture.is_some() => {
93 updated = updated || command_capture.as_mut().unwrap().right().is_ok()
146 updated = updated
147 || match scroll_behaviour {
148 TextbarScrollBehaviour::Word => {
149 next_word(
150 framework
151 .data
152 .global
153 .get_mut::<Status>()
154 .unwrap()
155 .command_capture
156 .as_mut()
157 .unwrap(),
158 );
159 true
160 }
161 TextbarScrollBehaviour::History => {
162 let status = framework.data.global.get::<Status>().unwrap();
163 if status.command_history_index.is_none() {
164 false
165 } else {
166 let history =
167 framework.data.global.get::<CommandHistory>().unwrap();
168 if status.command_history_index == Some(history.0.len() - 1)
169 {
170 let status =
171 framework.data.global.get_mut::<Status>().unwrap();
172 let text_field =
173 status.command_capture.as_mut().unwrap();
174 status.command_history_index = None;
175 text_field.content =
176 status.command_editing_cache.clone();
177 } else {
178 let new_index = status
179 .command_history_index
180 .unwrap_or(history.0.len())
181 + 1;
182 let new_content = history.0[new_index].clone();
183
184 let status =
185 framework.data.global.get_mut::<Status>().unwrap();
186 let text_field =
187 status.command_capture.as_mut().unwrap();
188 text_field.content = new_content;
189 status.command_history_index = Some(new_index);
190 let _ = text_field.last();
191 }
192 true
193 }
194 }
195 TextbarScrollBehaviour::Character => {
196 command_capture.as_mut().unwrap().right().is_ok()
197 }
198 }
94199 }
95200 MouseEventKind::ScrollDown => {
96201 let data: Box<dyn Any> = Box::new("scrolldown".to_string());