whisker-linux 0.19.3

Native GTK4 Linux Host for Whisker.
Documentation
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
use std::cell::{Cell, RefCell};
use std::rc::Rc;

use gtk::glib::translate::ToGlibPtr;
use gtk::prelude::*;
use gtk::{gdk, gio, glib};
use gtk4 as gtk;
use whisker::runtime::RuntimeWakeHandle;
use whisker::runtime::module::RustModuleRuntime;
use whisker::{Element, ElementRegistry, RuntimeInstance, SurfaceRuntime};
use whisker_engine::LayoutOptions;
use whisker_protocol::{
    CapabilityEntry, CapabilitySupport, InputEvent, InputEventKind, InputPoint, NodeId, PointerId,
    PointerInput, PointerKind, ProtocolVersion, RenderCapabilities, RenderCapability, SurfaceId,
    WhiskerValue,
};
use whisker_style::StyleEnvironment;

use crate::node::NodeWidget;
use crate::scene::{GtkFrameSink, SharedScrollFeedback, max_scroll_offset};
use crate::text::PangoMeasurementProvider;
use crate::{LinuxAppConfig, LinuxError, LinuxModuleDefinition};

type ViewFrame = Box<dyn FnOnce(f64)>;

thread_local! {
    static APPLICATION: RefCell<Option<LinuxApplication>> = const { RefCell::new(None) };
    static FRAME_SCHEDULED: Cell<bool> = const { Cell::new(false) };
    static VIEW_FRAMES: RefCell<Vec<ViewFrame>> = const { RefCell::new(Vec::new()) };
    static RUNNING_VIEW_FRAMES: Cell<bool> = const { Cell::new(false) };
    // Separate from APPLICATION so wakes raised during a frame can still schedule one.
    static SURFACE: RefCell<Option<NodeWidget>> = const { RefCell::new(None) };
}

#[cfg(feature = "hot-reload")]
type HotReload = whisker_dev_runtime::NativeHotReload;

#[cfg(not(feature = "hot-reload"))]
struct HotReload;

#[cfg(not(feature = "hot-reload"))]
impl HotReload {
    fn new(
        _wake: RuntimeWakeHandle,
        _application: fn() -> Element,
        _application_hash: fn() -> u64,
    ) -> Self {
        Self
    }

    fn apply(&mut self, _runtime: &mut RuntimeInstance) -> Result<bool, String> {
        Ok(false)
    }

    fn record_frame(&self, _timing: Option<whisker::runtime::FrameTiming>) {}
}

const ROOT_FONT_SIZE: f32 = 14.0;
const MOUSE_POINTER: u64 = 1;

pub(crate) fn host_capabilities() -> RenderCapabilities {
    let native = [
        RenderCapability::EllipticalBorderRadius,
        RenderCapability::VisualEffects,
        RenderCapability::TextEffects,
        RenderCapability::TextTypography,
        RenderCapability::RichText,
        RenderCapability::Cursor,
        RenderCapability::ResourceLifecycle,
        RenderCapability::LinearGradients,
        RenderCapability::RadialGradients,
        RenderCapability::ConicGradients,
        RenderCapability::BackgroundGeometry,
        RenderCapability::BackgroundLayerStacking,
        RenderCapability::BackgroundImageResources,
    ]
    .map(|capability| CapabilityEntry {
        capability,
        support: CapabilitySupport::Native,
    });
    // GSK cannot sample pixels painted behind a node.
    let emulated = CapabilityEntry {
        capability: RenderCapability::BackdropBlur,
        support: CapabilitySupport::Emulated,
    };
    RenderCapabilities::new(
        ProtocolVersion::CURRENT,
        native.into_iter().chain([emulated]),
    )
    .expect("GTK capability profile is unique")
}

pub(crate) fn run_application(
    config: LinuxAppConfig,
    application: fn() -> Element,
    application_hash: fn() -> u64,
) -> Result<(), LinuxError> {
    gtk::init().map_err(|error| LinuxError(format!("initialize GTK: {error}")))?;
    let app = gtk::Application::builder()
        .flags(gio::ApplicationFlags::NON_UNIQUE)
        .build();
    let failure = Rc::new(RefCell::new(None));
    {
        let failure = Rc::clone(&failure);
        app.connect_activate(move |app| {
            if let Err(error) = activate(app, &config, application, application_hash) {
                *failure.borrow_mut() = Some(error);
                app.quit();
            }
        });
    }
    app.run_with_args::<&str>(&[]);
    SURFACE.with(|slot| slot.borrow_mut().take());
    APPLICATION.with(|slot| slot.borrow_mut().take());
    match failure.borrow_mut().take() {
        Some(error) => Err(error),
        None => Ok(()),
    }
}

fn activate(
    app: &gtk::Application,
    config: &LinuxAppConfig,
    application: fn() -> Element,
    application_hash: fn() -> u64,
) -> Result<(), LinuxError> {
    let surface = NodeWidget::new();
    surface.state_mut().hit_target = true;
    surface.set_hexpand(true);
    surface.set_vexpand(true);
    let [red, green, blue] = config.background_rgb;
    surface.state_mut().background = Some(gdk::RGBA::new(
        f32::from(red) / 255.0,
        f32::from(green) / 255.0,
        f32::from(blue) / 255.0,
        1.0,
    ));
    surface.set_resize_callback(request_frame);
    let window = gtk::ApplicationWindow::builder()
        .application(app)
        .title(config.title.as_str())
        .default_width(config.width)
        .default_height(config.height)
        .child(&surface)
        .build();

    let host = LinuxApplication::new(
        surface.clone(),
        &config.modules,
        application,
        application_hash,
    )?;
    APPLICATION.with(|slot| *slot.borrow_mut() = Some(host));
    SURFACE.with(|slot| *slot.borrow_mut() = Some(surface.clone()));
    with_application(|host| {
        host.modules
            .with_host(|| host.runtime.mount(application))
            .map(|_| ())
            .map_err(|error| LinuxError(format!("mount Whisker application: {error}")))
    })?;
    install_input(&surface);
    window.present();
    request_frame();
    Ok(())
}

fn with_application<T>(
    work: impl FnOnce(&mut LinuxApplication) -> Result<T, LinuxError>,
) -> Result<T, LinuxError> {
    APPLICATION.with(|slot| {
        let mut slot = slot.borrow_mut();
        let host = slot
            .as_mut()
            .ok_or_else(|| LinuxError("GTK application is not mounted".into()))?;
        work(host)
    })
}

struct LinuxApplication {
    surface: NodeWidget,
    runtime: RuntimeInstance,
    hot_reload: HotReload,
    measurements: PangoMeasurementProvider,
    frames: GtkFrameSink,
    feedback: SharedScrollFeedback,
    modules: RustModuleRuntime,
    viewport: (f32, f32, f32),
    viewport_epoch: u32,
    environment_epoch: u64,
}

impl LinuxApplication {
    fn new(
        surface: NodeWidget,
        definitions: &[LinuxModuleDefinition],
        application: fn() -> Element,
        application_hash: fn() -> u64,
    ) -> Result<Self, LinuxError> {
        let modules = RustModuleRuntime::new(
            definitions.iter().map(|module| module.service.clone()),
            request_frame,
        )
        .map_err(|error| LinuxError(format!("bind GTK modules: {error}")))?;
        let elements = ElementRegistry::standard_with_linked_providers()
            .map_err(|error| LinuxError(format!("build element registry: {error}")))?;
        let capabilities = host_capabilities()
            .negotiate(ProtocolVersion::CURRENT)
            .map_err(|error| LinuxError(format!("negotiate GTK Host capabilities: {error}")))?;
        let surface_id = SurfaceId::new(1).expect("the GTK surface id is non-zero");
        let viewport = (1.0, 1.0, surface.scale_factor() as f32);
        let runtime_surface = SurfaceRuntime::with_element_registry_and_protocol(
            surface_id,
            StyleEnvironment::new(viewport.0, viewport.1, viewport.2, ROOT_FONT_SIZE),
            elements.clone(),
            capabilities.protocol(),
        );
        let wake = RuntimeWakeHandle::new(|| glib::MainContext::default().invoke(request_frame));
        let views = definitions
            .iter()
            .flat_map(|module| module.views.iter().cloned())
            .collect::<Vec<_>>();
        let measurements = PangoMeasurementProvider::new(crate::text::new_context())
            .with_view_measurements(elements.registrations(), &views);
        let frames = GtkFrameSink::new(
            surface.clone(),
            surface_id,
            elements.registrations(),
            capabilities,
            measurements.prepared(),
            measurements.context().clone(),
            views,
        );
        Ok(Self {
            feedback: frames.feedback(),
            surface,
            runtime: RuntimeInstance::new(runtime_surface, wake.clone()),
            hot_reload: HotReload::new(wake, application, application_hash),
            measurements,
            frames,
            modules,
            viewport,
            viewport_epoch: 1,
            environment_epoch: 1,
        })
    }

    fn drive_frame(&mut self, timestamp_ms: f64) -> Result<bool, LinuxError> {
        let current = (
            self.surface.width() as f32,
            self.surface.height() as f32,
            self.surface.scale_factor() as f32,
        );
        if current.0 <= 0.0 || current.1 <= 0.0 {
            return Ok(false);
        }
        if current != self.viewport {
            self.viewport = current;
            self.viewport_epoch = self.viewport_epoch.wrapping_add(1).max(1);
            self.environment_epoch = self.environment_epoch.wrapping_add(1).max(1);
        }
        if let Err(error) = self
            .modules
            .with_host(|| self.hot_reload.apply(&mut self.runtime))
        {
            eprintln!("whisker: hot reload failed: {error}");
        }
        self.dispatch_provider_events(timestamp_ms)?;
        let drive = self
            .modules
            .with_host(|| {
                self.runtime.drive_frame(
                    timestamp_ms,
                    StyleEnvironment::new(
                        self.viewport.0,
                        self.viewport.1,
                        self.viewport.2,
                        ROOT_FONT_SIZE,
                    ),
                    self.environment_epoch,
                    self.viewport_epoch,
                    &mut self.measurements,
                    &mut self.frames,
                    LayoutOptions::default(),
                )
            })
            .map_err(|error| LinuxError(format!("drive GTK frame: {error}")))?;
        self.hot_reload.record_frame(drive.timing);
        self.modules
            .dispatch_pending_events(&self.runtime)
            .map_err(|error| LinuxError(format!("dispatch GTK module event: {error}")))?;
        Ok(drive.needs_frame)
    }

    fn dispatch_provider_events(&mut self, timestamp_ms: f64) -> Result<(), LinuxError> {
        let events = self.feedback.borrow_mut().take_events();
        for event in events {
            let presentation = self.feedback.borrow_mut().take_presentation_updates();
            self.modules
                .with_host(|| {
                    self.runtime.dispatch_input_with_presentation(
                        &InputEvent {
                            presentation_revision: None,
                            surface: self.runtime.surface().surface(),
                            timestamp_ms,
                            kind: InputEventKind::Named(event.name),
                            pointer: None,
                            target: Some(event.target),
                            detail: event.detail,
                        },
                        &presentation,
                    )
                })
                .map_err(|error| LinuxError(format!("dispatch GTK element event: {error}")))?;
        }
        Ok(())
    }

    fn dispatch_pointer(
        &mut self,
        kind: InputEventKind,
        pointer: PointerInput,
        timestamp_ms: f64,
    ) -> Result<(), LinuxError> {
        let presentation = self.feedback.borrow_mut().take_presentation_updates();
        let event = InputEvent {
            presentation_revision: None,
            surface: self.runtime.surface().surface(),
            timestamp_ms,
            kind,
            pointer: Some(pointer),
            target: None,
            detail: WhiskerValue::Null,
        };
        self.modules
            .with_host(|| {
                self.runtime
                    .dispatch_input_with_presentation(&event, &presentation)
            })
            .map(|_| ())
            .map_err(|error| LinuxError(format!("dispatch GTK pointer input: {error}")))
    }

    /// Dispatches a wheel with deltas in logical pixels and returns whether a catching listener took
    /// it, in which case the Host must not scroll.
    fn dispatch_wheel(
        &mut self,
        point: InputPoint,
        delta: [f32; 2],
        ctrl_key: bool,
        timestamp_ms: f64,
    ) -> Result<bool, LinuxError> {
        let mut detail = vec![
            ("deltaX", WhiskerValue::Float(f64::from(delta[0]))),
            ("deltaY", WhiskerValue::Float(f64::from(delta[1]))),
            ("ctrlKey", WhiskerValue::Bool(ctrl_key)),
        ];
        if !ctrl_key && let Some((scroller, _)) = self.scroller_at(point, delta) {
            detail.push(("scroller", WhiskerValue::Int(scroller.get() as i64)));
        }
        let presentation = self.feedback.borrow_mut().take_presentation_updates();
        let event = InputEvent {
            presentation_revision: None,
            surface: self.runtime.surface().surface(),
            timestamp_ms,
            kind: InputEventKind::Wheel,
            pointer: Some(PointerInput {
                id: PointerId::new(1).expect("non-zero"),
                kind: PointerKind::Mouse,
                position: point,
                buttons: 0,
                changed_button: -1,
            }),
            target: None,
            detail: WhiskerValue::map(detail),
        };
        self.modules
            .with_host(|| {
                self.runtime
                    .dispatch_input_with_presentation(&event, &presentation)
            })
            .map(|dispatch| dispatch.caught)
            .map_err(|error| LinuxError(format!("dispatch GTK wheel input: {error}")))
    }

    fn scroll_at(&mut self, point: InputPoint, delta: [f32; 2]) -> bool {
        self.scroller_at(point, delta)
            .is_some_and(|(id, offset)| self.frames.scroll_to(id, offset))
    }

    /// The innermost enabled ScrollView under `point` that can still move by `delta`, and the offset
    /// it would move to.
    fn scroller_at(&self, point: InputPoint, delta: [f32; 2]) -> Option<(NodeId, [f32; 2])> {
        let mut node = self
            .surface
            .hit_test(point.x, point.y)
            .and_then(|widget| widget.state().id);
        while let Some(id) = node {
            let scroll = self.frames.widget(id).and_then(|widget| {
                widget
                    .state()
                    .scroll
                    .map(|scroll| (scroll, max_scroll_offset(widget)))
            });
            if let Some((scroll, max)) = scroll
                && scroll.enabled
            {
                let axis = usize::from(!scroll.horizontal);
                let amount = if delta[axis] == 0.0 {
                    delta[1 - axis]
                } else {
                    delta[axis]
                };
                let target = (scroll.offset[axis] + amount).clamp(0.0, max[axis]);
                if target != scroll.offset[axis] {
                    let mut offset = scroll.offset;
                    offset[axis] = target;
                    return Some((id, offset));
                }
            }
            node = self.frames.parent(id);
        }
        None
    }
}

/// Calls `callback` with the frame clock time in milliseconds on the next frame, before Whisker
/// applies that frame's updates, so what it publishes lands in the same frame.
///
/// It runs outside Whisker's commit, so it may update the runtime.
pub fn request_view_frame(callback: impl FnOnce(f64) + 'static) {
    VIEW_FRAMES.with_borrow_mut(|frames| frames.push(Box::new(callback)));
    request_frame();
}

fn run_view_frames(timestamp_ms: f64) {
    let due = VIEW_FRAMES.with_borrow_mut(std::mem::take);
    if due.is_empty() {
        return;
    }
    RUNNING_VIEW_FRAMES.set(true);
    for callback in due {
        callback(timestamp_ms);
    }
    RUNNING_VIEW_FRAMES.set(false);
}

pub(crate) fn request_frame() {
    // The tick that follows the View frames picks up anything they wake.
    if RUNNING_VIEW_FRAMES.get() {
        return;
    }
    if FRAME_SCHEDULED.replace(true) {
        return;
    }
    let surface = SURFACE.with(|slot| slot.borrow().clone());
    let Some(surface) = surface else {
        FRAME_SCHEDULED.set(false);
        return;
    };
    surface.add_tick_callback(|_, clock| {
        FRAME_SCHEDULED.set(false);
        let timestamp_ms = clock.frame_time() as f64 / 1000.0;
        run_view_frames(timestamp_ms);
        match with_application(|host| host.drive_frame(timestamp_ms)) {
            Ok(true) => request_frame(),
            Ok(false) => {}
            Err(error) => eprintln!("whisker: {error}"),
        }
        if VIEW_FRAMES.with_borrow(|frames| !frames.is_empty()) {
            request_frame();
        }
        glib::ControlFlow::Break
    });
}

fn surface_point(widget: &NodeWidget, event: &gdk::Event) -> Option<InputPoint> {
    let (x, y) = event.position()?;
    let native = widget.native()?;
    let (offset_x, offset_y) = native.surface_transform();
    let point = native.compute_point(
        widget,
        &gtk::graphene::Point::new((x - offset_x) as f32, (y - offset_y) as f32),
    )?;
    Some(InputPoint {
        x: point.x(),
        y: point.y(),
    })
}

fn button_bit(button: u32) -> (i16, u32) {
    match button {
        1 => (0, 1),
        2 => (1, 4),
        3 => (2, 2),
        8 => (3, 8),
        9 => (4, 16),
        _ => (-1, 0),
    }
}

fn held_buttons(state: gdk::ModifierType) -> u32 {
    [
        (gdk::ModifierType::BUTTON1_MASK, 1),
        (gdk::ModifierType::BUTTON2_MASK, 4),
        (gdk::ModifierType::BUTTON3_MASK, 2),
        (gdk::ModifierType::BUTTON4_MASK, 8),
        (gdk::ModifierType::BUTTON5_MASK, 16),
    ]
    .into_iter()
    .filter(|(mask, _)| state.contains(*mask))
    .fold(0, |buttons, (_, bit)| buttons | bit)
}

fn translate_pointer(
    widget: &NodeWidget,
    event: &gdk::Event,
) -> Option<(InputEventKind, PointerInput)> {
    let position = surface_point(widget, event)?;
    let held = held_buttons(event.modifier_state());
    let mouse = |changed_button: i16, buttons: u32| PointerInput {
        id: PointerId::new(MOUSE_POINTER).expect("mouse pointer id is non-zero"),
        kind: PointerKind::Mouse,
        position,
        buttons,
        changed_button,
    };
    let touch = |buttons: u32| {
        let sequence = event.event_sequence();
        let sequence: *const gdk::ffi::GdkEventSequence = sequence.to_glib_none().0;
        let sequence = sequence as usize as u64;
        PointerInput {
            id: PointerId::new(sequence.wrapping_add(2).max(2)).expect("touch id is non-zero"),
            kind: PointerKind::Touch,
            position,
            buttons,
            changed_button: if buttons == 0 { 0 } else { -1 },
        }
    };
    match event.event_type() {
        gdk::EventType::ButtonPress => {
            let button = event.downcast_ref::<gdk::ButtonEvent>()?.button();
            let (changed, bit) = button_bit(button);
            Some((InputEventKind::PointerDown, mouse(changed, held | bit)))
        }
        gdk::EventType::ButtonRelease => {
            let button = event.downcast_ref::<gdk::ButtonEvent>()?.button();
            let (changed, bit) = button_bit(button);
            Some((InputEventKind::PointerUp, mouse(changed, held & !bit)))
        }
        gdk::EventType::MotionNotify => Some((InputEventKind::PointerMove, mouse(-1, held))),
        gdk::EventType::TouchBegin => Some((InputEventKind::PointerDown, touch(1))),
        gdk::EventType::TouchUpdate => Some((InputEventKind::PointerMove, touch(1))),
        gdk::EventType::TouchEnd => Some((InputEventKind::PointerUp, touch(0))),
        gdk::EventType::TouchCancel => Some((InputEventKind::PointerCancel, touch(0))),
        _ => None,
    }
}

fn update_cursor(surface: &NodeWidget, point: InputPoint) {
    let node = surface
        .hit_test(point.x, point.y)
        .and_then(|widget| widget.state().id);
    let name = node.and_then(|node| {
        with_application(|host| Ok(host.frames.cursor_for(node)))
            .ok()
            .flatten()
    });
    surface.set_cursor_from_name(name);
}

fn install_input(surface: &NodeWidget) {
    // Some backends, such as Broadway, deliver wheel events without a position.
    let last_pointer = Rc::new(Cell::new(InputPoint { x: 0.0, y: 0.0 }));
    let legacy = gtk::EventControllerLegacy::new();
    {
        let surface = surface.clone();
        let last_pointer = Rc::clone(&last_pointer);
        legacy.connect_event(move |_, event| {
            if let Some((kind, pointer)) = translate_pointer(&surface, event) {
                last_pointer.set(pointer.position);
                if kind == InputEventKind::PointerMove {
                    update_cursor(&surface, pointer.position);
                }
                let timestamp_ms = f64::from(event.time());
                if let Err(error) =
                    with_application(|host| host.dispatch_pointer(kind, pointer, timestamp_ms))
                {
                    eprintln!("whisker: {error}");
                }
            }
            glib::Propagation::Proceed
        });
    }
    surface.add_controller(legacy);

    let scroll = gtk::EventControllerScroll::new(gtk::EventControllerScrollFlags::BOTH_AXES);
    let target = surface.clone();
    scroll.connect_scroll(move |controller, dx, dy| {
        let wheel = controller.unit() == gdk::ScrollUnit::Wheel;
        let step = if wheel { 48.0 } else { 1.0 };
        let delta = [dx as f32 * step, dy as f32 * step];
        let point = controller
            .current_event()
            .and_then(|event| surface_point(&target, &event))
            .unwrap_or_else(|| last_pointer.get());
        let ctrl_key = controller
            .current_event_state()
            .contains(gdk::ModifierType::CONTROL_MASK);
        let timestamp_ms = f64::from(controller.current_event_time());
        let moved = with_application(|host| {
            Ok(host.dispatch_wheel(point, delta, ctrl_key, timestamp_ms)?
                || host.scroll_at(point, delta))
        })
        .unwrap_or_else(|error| {
            eprintln!("whisker: {error}");
            false
        });
        if moved {
            request_frame();
            glib::Propagation::Stop
        } else {
            glib::Propagation::Proceed
        }
    });
    surface.add_controller(scroll);
}