Commit 82ed796a91

82ed796a91605c31c231d9f3c52b8cae837106bd

parent: 3296f58859

Unregistered key

Aleš Katona <ales@katona.me> · 2025-05-29 04:48 UTC
committer: <noreply@github.com>

Add support for scrolling w/ mouse when explicitly enabled (#389)

Co-authored-by: Ulyssa <git@ulyssa.dev>

Layout: unified · split

src/config.rs +10
@@ -466,6 +466,12 @@ impl<'de> Deserialize<'de> for NotifyVia {
466 } 466 }
467} 467}
468 468
469#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
470pub struct Mouse {
471 #[serde(default)]
472 pub enabled: bool,
473}
474
469#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] 475#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
470pub struct Notifications { 476pub struct Notifications {
471 #[serde(default)] 477 #[serde(default)]
@@ -562,6 +568,7 @@ pub struct TunableValues {
562 pub message_user_color: bool, 568 pub message_user_color: bool,
563 pub default_room: Option<String>, 569 pub default_room: Option<String>,
564 pub open_command: Option<Vec<String>>, 570 pub open_command: Option<Vec<String>>,
571 pub mouse: Mouse,
565 pub notifications: Notifications, 572 pub notifications: Notifications,
566 pub image_preview: Option<ImagePreviewValues>, 573 pub image_preview: Option<ImagePreviewValues>,
567 pub user_gutter_width: usize, 574 pub user_gutter_width: usize,
@@ -586,6 +593,7 @@ pub struct Tunables {
586 pub message_user_color: Option<bool>, 593 pub message_user_color: Option<bool>,
587 pub default_room: Option<String>, 594 pub default_room: Option<String>,
588 pub open_command: Option<Vec<String>>, 595 pub open_command: Option<Vec<String>>,
596 pub mouse: Option<Mouse>,
589 pub notifications: Option<Notifications>, 597 pub notifications: Option<Notifications>,
590 pub image_preview: Option<ImagePreview>, 598 pub image_preview: Option<ImagePreview>,
591 pub user_gutter_width: Option<usize>, 599 pub user_gutter_width: Option<usize>,
@@ -614,6 +622,7 @@ impl Tunables {
614 message_user_color: self.message_user_color.or(other.message_user_color), 622 message_user_color: self.message_user_color.or(other.message_user_color),
615 default_room: self.default_room.or(other.default_room), 623 default_room: self.default_room.or(other.default_room),
616 open_command: self.open_command.or(other.open_command), 624 open_command: self.open_command.or(other.open_command),
625 mouse: self.mouse.or(other.mouse),
617 notifications: self.notifications.or(other.notifications), 626 notifications: self.notifications.or(other.notifications),
618 image_preview: self.image_preview.or(other.image_preview), 627 image_preview: self.image_preview.or(other.image_preview),
619 user_gutter_width: self.user_gutter_width.or(other.user_gutter_width), 628 user_gutter_width: self.user_gutter_width.or(other.user_gutter_width),
@@ -640,6 +649,7 @@ impl Tunables {
640 message_user_color: self.message_user_color.unwrap_or(false), 649 message_user_color: self.message_user_color.unwrap_or(false),
641 default_room: self.default_room, 650 default_room: self.default_room,
642 open_command: self.open_command, 651 open_command: self.open_command,
652 mouse: self.mouse.unwrap_or_default(),
643 notifications: self.notifications.unwrap_or_default(), 653 notifications: self.notifications.unwrap_or_default(),
644 image_preview: self.image_preview.map(ImagePreview::values), 654 image_preview: self.image_preview.map(ImagePreview::values),
645 user_gutter_width: self.user_gutter_width.unwrap_or(30), 655 user_gutter_width: self.user_gutter_width.unwrap_or(30),
src/main.rs +42 −8
@@ -44,11 +44,14 @@ use modalkit::crossterm::{
44 read, 44 read,
45 DisableBracketedPaste, 45 DisableBracketedPaste,
46 DisableFocusChange, 46 DisableFocusChange,
47 DisableMouseCapture,
47 EnableBracketedPaste, 48 EnableBracketedPaste,
48 EnableFocusChange, 49 EnableFocusChange,
50 EnableMouseCapture,
49 Event, 51 Event,
50 KeyEventKind, 52 KeyEventKind,
51 KeyboardEnhancementFlags, 53 KeyboardEnhancementFlags,
54 MouseEventKind,
52 PopKeyboardEnhancementFlags, 55 PopKeyboardEnhancementFlags,
53 PushKeyboardEnhancementFlags, 56 PushKeyboardEnhancementFlags,
54 }, 57 },
@@ -364,8 +367,30 @@ impl Application {
364 367
365 return Ok(ke.into()); 368 return Ok(ke.into());
366 }, 369 },
367 Event::Mouse(_) => { 370 Event::Mouse(me) => {
368 // Do nothing for now. 371 let dir = match me.kind {
372 MouseEventKind::ScrollUp => MoveDir2D::Up,
373 MouseEventKind::ScrollDown => MoveDir2D::Down,
374 MouseEventKind::ScrollLeft => MoveDir2D::Left,
375 MouseEventKind::ScrollRight => MoveDir2D::Right,
376 _ => continue,
377 };
378
379 let size = ScrollSize::Cell;
380 let style = ScrollStyle::Direction2D(dir, size, 1.into());
381 let ctx = ProgramContext::default();
382 let mut store = self.store.lock().await;
383
384 match self.screen.scroll(&style, &ctx, store.deref_mut()) {
385 Ok(None) => {},
386 Ok(Some(info)) => {
387 drop(store);
388 self.handle_info(info);
389 },
390 Err(e) => {
391 self.screen.push_error(e);
392 },
393 }
369 }, 394 },
370 Event::FocusGained => { 395 Event::FocusGained => {
371 let mut store = self.store.lock().await; 396 let mut store = self.store.lock().await;
@@ -932,8 +957,8 @@ async fn login_normal(
932} 957}
933 958
934/// Set up the terminal for drawing the TUI, and getting additional info. 959/// Set up the terminal for drawing the TUI, and getting additional info.
935fn setup_tty(title: &str, enable_enhanced_keys: bool) -> std::io::Result<()> { 960fn setup_tty(settings: &ApplicationSettings, enable_enhanced_keys: bool) -> std::io::Result<()> {
936 let title = format!("iamb ({})", title); 961 let title = format!("iamb ({})", settings.profile.user_id.as_str());
937 962
938 // Enable raw mode and enter the alternate screen. 963 // Enable raw mode and enter the alternate screen.
939 crossterm::terminal::enable_raw_mode()?; 964 crossterm::terminal::enable_raw_mode()?;
@@ -947,15 +972,23 @@ fn setup_tty(title: &str, enable_enhanced_keys: bool) -> std::io::Result<()> {
947 )?; 972 )?;
948 } 973 }
949 974
975 if settings.tunables.mouse.enabled {
976 crossterm::execute!(stdout(), EnableMouseCapture)?;
977 }
978
950 crossterm::execute!(stdout(), EnableBracketedPaste, EnableFocusChange, SetTitle(title)) 979 crossterm::execute!(stdout(), EnableBracketedPaste, EnableFocusChange, SetTitle(title))
951} 980}
952 981
953// Do our best to reverse what we did in setup_tty() when we exit or crash. 982// Do our best to reverse what we did in setup_tty() when we exit or crash.
954fn restore_tty(enable_enhanced_keys: bool) { 983fn restore_tty(enable_enhanced_keys: bool, enable_mouse: bool) {
955 if enable_enhanced_keys { 984 if enable_enhanced_keys {
956 let _ = crossterm::queue!(stdout(), PopKeyboardEnhancementFlags); 985 let _ = crossterm::queue!(stdout(), PopKeyboardEnhancementFlags);
957 } 986 }
958 987
988 if enable_mouse {
989 let _ = crossterm::queue!(stdout(), DisableMouseCapture);
990 }
991
959 let _ = crossterm::execute!( 992 let _ = crossterm::execute!(
960 stdout(), 993 stdout(),
961 DisableBracketedPaste, 994 DisableBracketedPaste,
@@ -1009,11 +1042,12 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
1009 false 1042 false
1010 }, 1043 },
1011 }; 1044 };
1012 setup_tty(settings.profile.user_id.as_str(), enable_enhanced_keys)?; 1045 setup_tty(&settings, enable_enhanced_keys)?;
1013 1046
1014 let orig_hook = std::panic::take_hook(); 1047 let orig_hook = std::panic::take_hook();
1048 let enable_mouse = settings.tunables.mouse.enabled;
1015 std::panic::set_hook(Box::new(move |panic_info| { 1049 std::panic::set_hook(Box::new(move |panic_info| {
1016 restore_tty(enable_enhanced_keys); 1050 restore_tty(enable_enhanced_keys, enable_mouse);
1017 orig_hook(panic_info); 1051 orig_hook(panic_info);
1018 process::exit(1); 1052 process::exit(1);
1019 })); 1053 }));
@@ -1023,7 +1057,7 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
1023 application.run().await?; 1057 application.run().await?;
1024 1058
1025 // Clean up the terminal on exit. 1059 // Clean up the terminal on exit.
1026 restore_tty(enable_enhanced_keys); 1060 restore_tty(enable_enhanced_keys, enable_mouse);
1027 1061
1028 Ok(()) 1062 Ok(())
1029} 1063}
src/tests.rs +1
@@ -189,6 +189,7 @@ pub fn mock_tunables() -> TunableValues {
189 external_edit_file_suffix: String::from(".md"), 189 external_edit_file_suffix: String::from(".md"),
190 username_display: UserDisplayStyle::Username, 190 username_display: UserDisplayStyle::Username,
191 message_user_color: false, 191 message_user_color: false,
192 mouse: Default::default(),
192 notifications: Notifications { 193 notifications: Notifications {
193 enabled: false, 194 enabled: false,
194 via: NotifyVia::default(), 195 via: NotifyVia::default(),