-
-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathapplication.rs
More file actions
2262 lines (2081 loc) · 95 KB
/
Copy pathapplication.rs
File metadata and controls
2262 lines (2081 loc) · 95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::event::{ClickState, EventPayload, EventProxy, RioEvent, RioEventType};
use crate::ime::Preedit;
use crate::renderer::utils::update_colors_based_on_theme;
use crate::router::{routes::RoutePath, Router};
use crate::scheduler::{Scheduler, TimerId, Topic};
use crate::screen::touch::on_touch;
use crate::watcher::configuration_file_updates;
#[cfg(all(
feature = "audio",
not(target_os = "macos"),
not(target_os = "windows")
))]
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use raw_window_handle::HasDisplayHandle;
use rio_backend::clipboard::{Clipboard, ClipboardType};
use rio_backend::config::colors::{ColorRgb, NamedColor};
use rio_window::application::ApplicationHandler;
use rio_window::event::{
ElementState, Ime, MouseButton, MouseScrollDelta, StartCause, TouchPhase, WindowEvent,
};
use rio_window::event_loop::ActiveEventLoop;
use rio_window::event_loop::ControlFlow;
use rio_window::event_loop::{DeviceEvents, EventLoop};
#[cfg(target_os = "macos")]
use rio_window::platform::macos::ActiveEventLoopExtMacOS;
#[cfg(target_os = "macos")]
use rio_window::platform::macos::WindowExtMacOS;
use rio_window::window::WindowId;
use rio_window::window::{CursorIcon, Fullscreen};
use std::error::Error;
use std::time::{Duration, Instant};
pub struct Application<'a> {
config: rio_backend::config::Config,
event_proxy: EventProxy,
router: Router<'a>,
scheduler: Scheduler,
app_id: Option<String>,
global_hotkey: Option<crate::global_hotkey::GlobalHotkeys>,
/// Frontmost app when the quake window was shown, re-activated
/// when it hides so focus returns where the user was.
#[cfg(target_os = "macos")]
quake_previous_app: Option<i32>,
}
impl Application<'_> {
pub fn new<'app>(
config: rio_backend::config::Config,
config_error: Option<rio_backend::config::ConfigError>,
event_loop: &EventLoop<EventPayload>,
app_id: Option<String>,
) -> Application<'app> {
// SAFETY: Since this takes a pointer to the winit event loop, it MUST be dropped first,
// which is done in `exiting`.
let clipboard =
unsafe { Clipboard::new(event_loop.display_handle().unwrap().as_raw()) };
let mut router = Router::new(config.fonts.to_owned(), clipboard);
if let Some(error) = config_error {
router.propagate_error_to_next_route(error.into());
}
let proxy = event_loop.create_proxy();
let event_proxy = EventProxy::new(proxy.clone());
let _ = configuration_file_updates(
rio_backend::config::config_dir_path(),
event_proxy.clone(),
);
let scheduler = Scheduler::new(proxy);
event_loop.listen_device_events(DeviceEvents::Never);
#[cfg(any(target_os = "macos", target_os = "windows"))]
event_loop.set_confirm_before_quit(config.confirm_before_quit);
rio_notifier::request_authorization();
Application {
config,
event_proxy,
router,
scheduler,
app_id,
global_hotkey: None,
#[cfg(target_os = "macos")]
quake_previous_app: None,
}
}
fn skip_window_event(event: &WindowEvent) -> bool {
matches!(
event,
WindowEvent::KeyboardInput {
is_synthetic: true,
..
} | WindowEvent::ActivationTokenDone { .. }
| WindowEvent::DoubleTapGesture { .. }
| WindowEvent::TouchpadPressure { .. }
| WindowEvent::RotationGesture { .. }
| WindowEvent::CursorEntered { .. }
| WindowEvent::PinchGesture { .. }
| WindowEvent::AxisMotion { .. }
| WindowEvent::PanGesture { .. }
| WindowEvent::HoveredFileCancelled
| WindowEvent::Destroyed
| WindowEvent::HoveredFile(_)
| WindowEvent::Moved(_)
)
}
fn handle_audio_bell(&mut self) {
#[cfg(target_os = "macos")]
{
// Use system bell sound on macOS
unsafe {
#[link(name = "AppKit", kind = "framework")]
extern "C" {
fn NSBeep();
}
NSBeep();
}
}
#[cfg(target_os = "windows")]
{
// Use MessageBeep on Windows with MB_OK (0x00000000) for default beep
unsafe {
windows_sys::Win32::System::Diagnostics::Debug::MessageBeep(0x00000000);
}
}
#[cfg(all(not(target_os = "macos"), not(target_os = "windows")))]
{
#[cfg(feature = "audio")]
{
std::thread::spawn(|| {
if let Err(e) = play_bell_sound() {
tracing::warn!("Failed to play bell sound: {}", e);
}
});
}
#[cfg(not(feature = "audio"))]
{
tracing::debug!("Audio bell requested but audio feature is not enabled");
}
}
}
fn handle_desktop_notification(&self, title: &str, body: &str) {
rio_notifier::send_notification(title, body);
}
pub fn run(
&mut self,
event_loop: EventLoop<EventPayload>,
) -> Result<(), Box<dyn Error>> {
let result = event_loop.run_app(self);
result.map_err(Into::into)
}
}
impl Application<'_> {
/// Register a system-wide hotkey for every `ToggleQuake` binding
/// in the config, so the quake window opens while Rio is
/// unfocused. No-op when quake is not bound; pure Wayland has no
/// global hotkey API, the compositor keybinding + a regular
/// binding cover it there.
fn setup_quake_hotkey(&mut self) {
// Drop any previous manager first: registering a chord the old
// manager still holds fails on Windows and X11.
self.global_hotkey = None;
self.global_hotkey = crate::global_hotkey::setup(
self.event_proxy.clone(),
&self.config.bindings.keys,
);
}
/// The monitor the quake window should drop down on: the one
/// under the mouse cursor where the platform can tell us, the
/// primary monitor otherwise.
fn quake_monitor(
&self,
event_loop: &ActiveEventLoop,
) -> Option<rio_window::monitor::MonitorHandle> {
event_loop
.cursor_monitor()
.or_else(|| event_loop.primary_monitor())
}
/// Anchor the quake window to the top of `monitor`, horizontally
/// centered, sized by the configured percentages, then show it.
fn show_quake_window(
&mut self,
id: rio_window::window::WindowId,
event_loop: &ActiveEventLoop,
) {
#[cfg(target_os = "macos")]
{
self.quake_previous_app =
rio_window::platform::macos::frontmost_application_pid();
}
let Some(route) = self.router.routes.get(&id) else {
return;
};
let window = &route.window.winit_window;
if let Some(monitor) = self.quake_monitor(event_loop) {
let msize = monitor.size();
let mpos = monitor.position();
let width = (msize.width as f32
* self.config.window.quake_width_percentage.clamp(0.1, 1.0))
as u32;
let height = (msize.height as f32
* self.config.window.quake_height_percentage.clamp(0.1, 1.0))
as u32;
let x = mpos.x + (msize.width.saturating_sub(width) / 2) as i32;
let _ = window
.request_inner_size(rio_window::dpi::PhysicalSize::new(width, height));
window.set_outer_position(rio_window::dpi::PhysicalPosition::new(x, mpos.y));
}
window.set_visible(true);
window.focus_window();
}
/// Show, focus or hide the quake window; create it on first use.
fn toggle_quake_window(&mut self, event_loop: &ActiveEventLoop) {
let quake_id = self
.router
.quake_window_id
.filter(|id| self.router.routes.contains_key(id));
let Some(id) = quake_id else {
self.router.quake_window_id = None;
self.router.create_quake_window(
event_loop,
self.event_proxy.clone(),
&self.config,
);
if let Some(id) = self.router.quake_window_id {
self.show_quake_window(id, event_loop);
}
return;
};
if let Some(route) = self.router.routes.get_mut(&id) {
let window = &route.window.winit_window;
let visible = window.is_visible().unwrap_or(true);
if !visible {
self.show_quake_window(id, event_loop);
} else if window.has_focus() {
window.set_visible(false);
#[cfg(target_os = "macos")]
if let Some(pid) = self.quake_previous_app.take() {
rio_window::platform::macos::activate_application(pid);
}
} else {
self.show_quake_window(id, event_loop);
}
}
}
}
impl ApplicationHandler<EventPayload> for Application<'_> {
fn resumed(&mut self, _active_event_loop: &ActiveEventLoop) {}
fn new_events(&mut self, event_loop: &ActiveEventLoop, cause: StartCause) {
if cause != StartCause::Init
&& cause != StartCause::CreateWindow
&& cause != StartCause::MacOSReopen
{
return;
}
if cause == StartCause::MacOSReopen && !self.router.routes.is_empty() {
// Reopen (dock click) with every window minimized should
// restore one; otherwise clicking the dock icon does
// nothing at all.
let all_minimized = self
.router
.routes
.values()
.all(|route| route.window.winit_window.is_minimized() == Some(true));
if all_minimized {
if let Some(route) = self.router.routes.values().next() {
route.window.winit_window.set_minimized(false);
route.window.winit_window.focus_window();
}
}
return;
}
#[cfg(all(
any(feature = "x11", feature = "wayland"),
unix,
not(any(target_os = "redox", target_family = "wasm", target_os = "macos"))
))]
if cause == StartCause::Init
&& self.config.adaptive_colors.is_some()
&& self.config.force_theme.is_none()
{
use rio_window::platform::linux::ActiveEventLoopExtLinux;
event_loop.start_system_theme_monitor();
}
let theme = self
.config
.force_theme
.map(|t| t.to_window_theme())
.or_else(|| event_loop.system_theme());
update_colors_based_on_theme(&mut self.config, theme);
self.router.create_window(
event_loop,
self.event_proxy.clone(),
&self.config,
None,
self.app_id.as_deref(),
);
if cause == StartCause::Init {
self.setup_quake_hotkey();
}
// Schedule title updates every 2s
let timer_id = TimerId::new(Topic::UpdateTitles, 0);
if !self.scheduler.scheduled(timer_id) {
self.scheduler.schedule(
EventPayload::new(RioEventType::Rio(RioEvent::UpdateTitles), unsafe {
rio_window::window::WindowId::dummy()
}),
Duration::from_secs(2),
true,
timer_id,
);
}
tracing::info!("Initialisation complete");
}
fn user_event(&mut self, event_loop: &ActiveEventLoop, event: EventPayload) {
let window_id = event.window_id;
match event.payload {
RioEventType::Rio(RioEvent::Render) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
// Skip rendering for unfocused windows if configured
if self.config.renderer.disable_unfocused_render
&& !route.window.is_focused
{
return;
}
// Skip rendering for occluded windows if configured, unless we need to render after occlusion
if self.config.renderer.disable_occluded_render
&& route.window.is_occluded
&& !route.window.needs_render_after_occlusion
{
return;
}
// Clear the one-time render flag if it was set
if route.window.needs_render_after_occlusion {
route.window.needs_render_after_occlusion = false;
}
route.request_redraw();
}
}
RioEventType::Rio(RioEvent::RenderRoute(route_id)) => {
if self.config.renderer.strategy.is_event_based() {
if let Some(route) = self.router.routes.get_mut(&window_id) {
// Skip rendering for unfocused windows if configured
if self.config.renderer.disable_unfocused_render
&& !route.window.is_focused
{
if route.window.screen.renderer.scrollbar.needs_redraw() {
route.request_redraw();
}
return;
}
// Skip rendering for occluded windows if configured, unless we need to render after occlusion
if self.config.renderer.disable_occluded_render
&& route.window.is_occluded
&& !route.window.needs_render_after_occlusion
{
return;
}
// Clear the one-time render flag if it was set
if route.window.needs_render_after_occlusion {
route.window.needs_render_after_occlusion = false;
}
// Mark the renderable content as needing to render
if let Some(ctx_item) =
route.window.screen.ctx_mut().get_by_route_id(route_id)
{
ctx_item.val.renderable_content.pending_update.set_dirty();
}
// Check if we need to throttle based on timing
if let Some(wait_duration) = route.window.wait_until() {
// We need to wait before rendering again
let timer_id = TimerId::new(Topic::RenderRoute, route_id);
let event = EventPayload::new(
RioEventType::Rio(RioEvent::Render),
window_id,
);
// Only schedule if not already scheduled
if !self.scheduler.scheduled(timer_id) {
self.scheduler.schedule(
event,
wait_duration,
false,
timer_id,
);
}
} else {
// We can render immediately
route.request_redraw();
}
}
}
}
RioEventType::Rio(RioEvent::TerminalDamaged(route_id)) => {
if self.config.renderer.strategy.is_event_based() {
if let Some(route) = self.router.routes.get_mut(&window_id) {
if self.config.renderer.disable_unfocused_render
&& !route.window.is_focused
{
return;
}
if self.config.renderer.disable_occluded_render
&& route.window.is_occluded
&& !route.window.needs_render_after_occlusion
{
return;
}
if let Some(ctx_item) =
route.window.screen.ctx_mut().get_by_route_id(route_id)
{
// Just mark dirty — damage will be extracted from
// the terminal when the renderer locks it.
ctx_item.val.renderable_content.pending_update.set_dirty();
route.request_redraw();
}
}
}
}
RioEventType::Rio(RioEvent::UpdateGraphics { route_id, queues }) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
// Process graphics directly in sugarloaf
let sugarloaf = &mut route.window.screen.sugarloaf;
// Atlas graphics (sixel/iTerm2) share the per-image
// texture store with kitty images, in a disjoint key
// namespace.
for graphic_data in queues.pending {
let key = crate::renderer::atlas_image_key(graphic_data.id.get());
sugarloaf.image_data.insert(
key,
rio_backend::sugarloaf::GraphicDataEntry::from_graphic_data(
graphic_data,
),
);
}
// Image textures (kitty) → separate store, no clone
for (image_id, graphic_data) in queues.pending_images {
sugarloaf.image_data.insert(
crate::renderer::kitty_image_key(image_id),
rio_backend::sugarloaf::GraphicDataEntry::from_graphic_data(
graphic_data,
),
);
}
// Removals arrive as final image keys (atlas refs
// dropped off scrollback, kitty evictions) and free
// both the pixel store and the cached GPU texture.
for key in queues.remove_queue {
sugarloaf.remove_image(key);
}
// Mark the panel dirty: the renderer skips non-dirty
// panels, so a bare redraw after the pixels arrive
// would no-op and leave the image blank until the
// next unrelated damage.
if let Some(ctx_item) =
route.window.screen.ctx_mut().get_by_route_id(route_id)
{
ctx_item.val.renderable_content.pending_update.set_dirty();
}
// Request a redraw to display the updated graphics
route.request_redraw();
}
}
RioEventType::Rio(RioEvent::PrepareUpdateConfig) => {
let timer_id = TimerId::new(Topic::UpdateConfig, 0);
let event = EventPayload::new(
RioEventType::Rio(RioEvent::UpdateConfig),
window_id,
);
if !self.scheduler.scheduled(timer_id) {
self.scheduler.schedule(
event,
Duration::from_millis(250),
false,
timer_id,
);
}
}
RioEventType::Rio(RioEvent::ReportToAssistant(error)) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
route.report_error(&error);
}
}
RioEventType::Rio(RioEvent::UpdateConfig) => {
let (config, config_error) = match rio_backend::config::Config::try_load()
{
Ok(config) => (config, None),
Err(error) => (rio_backend::config::Config::default(), Some(error)),
};
let has_font_updates = self.config.fonts != config.fonts;
let has_binding_updates = self.config.bindings != config.bindings;
let font_library_errors = if has_font_updates {
let new_font_library = rio_backend::sugarloaf::font::FontLibrary::new(
config.fonts.to_owned(),
);
*self.router.font_library = new_font_library.0;
new_font_library.1
} else {
None
};
self.config = config;
// Dropping the old manager unregisters its hotkeys, so
// ToggleQuake binding edits apply without restarting.
if has_binding_updates {
self.setup_quake_hotkey();
}
let mut has_checked_adaptive_colors = false;
for (_id, route) in self.router.routes.iter_mut() {
// Apply system theme to ensure colors are consistent
if !has_checked_adaptive_colors {
let system_theme = event_loop.system_theme();
let theme = self
.config
.force_theme
.map(|t| t.to_window_theme())
.or(system_theme);
update_colors_based_on_theme(&mut self.config, theme);
has_checked_adaptive_colors = true;
}
if has_font_updates {
if let Some(ref err) = font_library_errors {
route
.window
.screen
.context_manager
.report_error_fonts_not_found(
err.fonts_not_found.clone(),
);
}
}
route.update_config(
&self.config,
&self.router.font_library,
has_font_updates,
);
route.window.configure_window(&self.config);
if let Some(error) = &config_error {
route.report_error(&error.to_owned().into());
} else {
route.clear_errors();
}
route.request_redraw();
}
}
RioEventType::Rio(RioEvent::Exit | RioEvent::Quit) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
if self.config.confirm_before_quit {
route.confirm_quit();
} else {
route.quit();
}
}
}
RioEventType::Rio(RioEvent::GlyphProtocolInstalled {
route_id,
registry,
}) => {
if let Some(route) = self.router.routes.get(&window_id) {
route
.window
.screen
.sugarloaf
.font_library()
.install_glyph_registry(route_id, registry);
}
}
RioEventType::Rio(RioEvent::GlyphProtocolQuery { route_id, cp }) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
use rio_backend::ansi::glyph_protocol::{
format_query_response, QueryStatus,
};
let library = route.window.screen.sugarloaf.font_library();
let in_glossary = library
.glyph_registry_for(route_id)
.is_some_and(|r| r.contains(cp));
let in_system = library.covers_codepoint(cp);
let status = match (in_glossary, in_system) {
(true, true) => QueryStatus::Both,
(true, false) => QueryStatus::Glossary,
(false, true) => QueryStatus::System,
(false, false) => QueryStatus::Free,
};
let resp = format_query_response(cp, status);
if let Some(item) = route
.window
.screen
.context_manager
.current_grid_mut()
.get_by_route_id(route_id)
{
item.context_mut().messenger.send_bytes(resp.into_bytes());
}
}
}
RioEventType::Rio(RioEvent::CloseTerminal(route_id)) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
route
.window
.screen
.sugarloaf
.font_library()
.remove_glyph_registry(route_id);
if route
.window
.screen
.context_manager
.should_close_context_manager(
route_id,
&mut route.window.screen.sugarloaf,
)
{
self.router.routes.remove(&window_id);
// Unschedule pending events.
self.scheduler.unschedule_window(route_id);
if self.router.routes.is_empty() {
event_loop.exit();
}
} else {
let size = route.window.screen.context_manager.len();
route.window.screen.resize_top_or_bottom_line(size);
// Force a repaint of the post-close state. The PTY
// thread also queues a separate Render, but if that is
// processed before this CloseTerminal (or coalesced),
// the closed tab lingers on screen until some later
// event — looking "frozen" until you click another
// tab. mark_dirty + redraw makes the close show now.
route.window.screen.mark_dirty();
route.request_redraw();
}
}
}
RioEventType::Rio(RioEvent::CursorBlinkingChange) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
route.request_redraw();
}
}
RioEventType::Rio(RioEvent::CursorBlinkingChangeOnRoute(route_id)) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
if route_id == route.window.screen.ctx().current_route() {
// Cursor blink toggles the cursor sprite (a
// separate quad), not cell content — so we
// signal `CursorOnly` and the GPU emit skips
// per-row rebuild while the cursor uniform
// updates downstream.
route
.window
.screen
.ctx_mut()
.current_mut()
.renderable_content
.pending_update
.set_terminal_damage(
rio_backend::event::TerminalDamage::CursorOnly,
);
route.request_redraw();
}
}
}
RioEventType::Rio(RioEvent::ProgressReport(report)) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
if let Some(island) = &mut route.window.screen.renderer.island {
island.set_progress_report(report);
route.request_redraw();
}
}
}
RioEventType::Rio(RioEvent::SelectionScrollTick) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
route.window.screen.selection_scroll_tick();
route.request_redraw();
}
}
RioEventType::Rio(RioEvent::Bell) => {
// Handle audio bell
if self.config.bell.audio {
self.handle_audio_bell();
}
}
RioEventType::Rio(RioEvent::DesktopNotification { title, body }) => {
self.handle_desktop_notification(&title, &body);
}
RioEventType::Rio(RioEvent::PrepareRender(millis)) => {
if let Some(route) = self.router.routes.get(&window_id) {
let timer_id = TimerId::new(
Topic::Render,
route.window.screen.ctx().current_route(),
);
let event =
EventPayload::new(RioEventType::Rio(RioEvent::Render), window_id);
if !self.scheduler.scheduled(timer_id) {
self.scheduler.schedule(
event,
Duration::from_millis(millis),
false,
timer_id,
);
}
}
}
RioEventType::Rio(RioEvent::PrepareRenderOnRoute(millis, route_id)) => {
let timer_id = TimerId::new(Topic::ScheduledRenderRoute, route_id);
let event = EventPayload::new(
RioEventType::Rio(RioEvent::RenderRoute(route_id)),
window_id,
);
if !self.scheduler.scheduled(timer_id) {
self.scheduler.schedule(
event,
Duration::from_millis(millis),
false,
timer_id,
);
}
}
RioEventType::Rio(RioEvent::BlinkCursor(millis, route_id)) => {
let timer_id = TimerId::new(Topic::CursorBlinking, route_id);
let event = EventPayload::new(
RioEventType::Rio(RioEvent::CursorBlinkingChangeOnRoute(route_id)),
window_id,
);
if !self.scheduler.scheduled(timer_id) {
self.scheduler.schedule(
event,
Duration::from_millis(millis),
false,
timer_id,
);
}
}
RioEventType::Rio(RioEvent::Title(title)) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
route.set_window_title(&title);
}
}
RioEventType::Rio(RioEvent::TitleWithSubtitle(title, subtitle)) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
route.set_window_title(&title);
route.set_window_subtitle(&subtitle);
}
}
RioEventType::Rio(RioEvent::UpdateTitles) => {
self.router.update_titles();
}
RioEventType::Rio(RioEvent::MouseCursorDirty) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
route.window.screen.reset_mouse();
}
}
RioEventType::Rio(RioEvent::Scroll(scroll)) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
let mut terminal = route
.window
.screen
.context_manager
.current_mut()
.terminal
.lock();
terminal.scroll_display(scroll);
drop(terminal);
}
}
RioEventType::Rio(RioEvent::ClipboardLoad(
route_id,
clipboard_type,
format,
)) => {
let Router {
routes, clipboard, ..
} = &mut self.router;
if let Some(route) = routes.get_mut(&window_id) {
if route.window.is_focused {
let text = format(clipboard.get(clipboard_type).as_str());
// Route the paste back to the panel that asked for it
// (OSC 52 reply), not whichever panel happens to be
// focused now.
if let Some(item) = route
.window
.screen
.context_manager
.get_by_route_id(route_id)
{
item.val.messenger.send_bytes(text.into_bytes());
}
}
}
}
RioEventType::Rio(RioEvent::ClipboardStore(clipboard_type, content)) => {
let Router {
routes, clipboard, ..
} = &mut self.router;
if let Some(route) = routes.get_mut(&window_id) {
if route.window.is_focused {
clipboard.set(clipboard_type, content);
}
}
}
RioEventType::Rio(RioEvent::PtyWrite(route_id, text)) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
// Route reply bytes (CSI / OSC responses) back to the
// PTY of the panel that emitted them, not whichever
// panel happens to be focused.
if let Some(item) = route
.window
.screen
.context_manager
.get_by_route_id(route_id)
{
item.val.messenger.send_bytes(text.into_bytes());
}
}
}
RioEventType::Rio(RioEvent::TextAreaSizeRequest(route_id, format)) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
if let Some(item) = route
.window
.screen
.context_manager
.get_by_route_id(route_id)
{
let dimension = item.val.dimension;
let text = format(crate::renderer::utils::terminal_dimensions(
&dimension,
));
item.val.messenger.send_bytes(text.into_bytes());
}
}
}
RioEventType::Rio(RioEvent::ColorRequest(route_id, index, format)) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
// Read the originating panel's terminal colors and
// route the reply back to that same panel — color
// theme overrides via OSC 4 / OSC 10-19 are
// per-context, so reading from `current()` would
// mis-report when the user has focused a different
// split mid-flight.
let renderer_color = route.window.screen.renderer.colors[index];
let Some(item) = route
.window
.screen
.context_manager
.get_by_route_id(route_id)
else {
return;
};
let terminal = item.val.terminal.lock();
let color: ColorRgb = match terminal.colors()[index] {
Some(color) => ColorRgb::from_color_arr(color),
// Ignore cursor color requests unless it was changed.
None if index
== crate::crosswords::NamedColor::Cursor as usize =>
{
return
}
None => ColorRgb::from_color_arr(renderer_color),
};
drop(terminal);
item.val.messenger.send_bytes(format(color).into_bytes());
}
}
RioEventType::Rio(RioEvent::CreateWindow) => {
self.router.create_window(
event_loop,
self.event_proxy.clone(),
&self.config,
None,
self.app_id.as_deref(),
);
}
RioEventType::Rio(RioEvent::ToggleQuake) => {
self.toggle_quake_window(event_loop);
}
#[cfg(target_os = "macos")]
RioEventType::Rio(RioEvent::CreateNativeTab(working_dir_overwrite)) => {
if let Some(route) = self.router.routes.get(&window_id) {
// This case happens only for native tabs
// every time that a new tab is created through context
// it also reaches for the foreground process path if
// config.use_current_path is true
// For these case we need to make a workaround
let config = if working_dir_overwrite.is_some() {
rio_backend::config::Config {
working_dir: working_dir_overwrite,
..self.config.clone()
}
} else {
self.config.clone()
};
self.router.create_native_tab(
event_loop,
self.event_proxy.clone(),
&config,
Some(&route.window.winit_window.tabbing_identifier()),
None,
);
}
}
RioEventType::Rio(RioEvent::CreateConfigEditor) => {
if self.config.navigation.open_config_with_split {
self.router.open_config_split(&self.config);
} else {
self.router.open_config_window(
event_loop,
self.event_proxy.clone(),
&self.config,
);
}
}
#[cfg(target_os = "macos")]
RioEventType::Rio(RioEvent::CloseWindow) => {
self.router.routes.remove(&window_id);
if self.router.routes.is_empty() && !self.config.confirm_before_quit {
event_loop.exit();
}
}
#[cfg(target_os = "macos")]
RioEventType::Rio(RioEvent::SelectNativeTabByIndex(tab_index)) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
route.window.winit_window.select_tab_at_index(tab_index);
}
}
#[cfg(target_os = "macos")]
RioEventType::Rio(RioEvent::SelectNativeTabLast) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
route
.window
.winit_window
.select_tab_at_index(route.window.winit_window.num_tabs() - 1);
}
}
#[cfg(target_os = "macos")]
RioEventType::Rio(RioEvent::SelectNativeTabNext) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
route.window.winit_window.select_next_tab();
}
}
#[cfg(target_os = "macos")]
RioEventType::Rio(RioEvent::SelectNativeTabPrev) => {
if let Some(route) = self.router.routes.get_mut(&window_id) {
route.window.winit_window.select_previous_tab();
}
}
#[cfg(target_os = "macos")]
RioEventType::Rio(RioEvent::Hide) => {
event_loop.hide_application();