iced_winit/application/
drag_resize.rs

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
use winit::window::{CursorIcon, ResizeDirection};

#[cfg(any(
    all(
        unix,
        not(target_vendor = "apple"),
        not(target_os = "android"),
        not(target_os = "emscripten"),
    ),
    target_os = "windows",
))]
const DRAG_RESIZE_SUPPORTED: bool = true;

#[cfg(not(any(
    all(
        unix,
        not(target_vendor = "apple"),
        not(target_os = "android"),
        not(target_os = "emscripten"),
    ),
    target_os = "windows",
)))]
const DRAG_RESIZE_SUPPORTED: bool = false;

/// If supported by winit, returns a closure that implements cursor resize support.
pub fn event_func(
    window: &dyn winit::window::Window,
    border_size: f64,
) -> Option<
    Box<
        dyn FnMut(
            &dyn winit::window::Window,
            &winit::event::WindowEvent,
        ) -> bool,
    >,
> {
    if DRAG_RESIZE_SUPPORTED {
        // Keep track of cursor when it is within a resizeable border.
        let mut cursor_prev_resize_direction = None;

        Some(Box::new(
            move |window: &dyn winit::window::Window,
                  window_event: &winit::event::WindowEvent|
                  -> bool {
                // Keep track of border resize state and set cursor icon when in range
                match window_event {
                    winit::event::WindowEvent::CursorMoved {
                        position, ..
                    } => {
                        if !window.is_decorated() {
                            let location = cursor_resize_direction(
                                window.surface_size(),
                                *position,
                                border_size,
                            );
                            if location != cursor_prev_resize_direction {
                                window.set_cursor(
                                    resize_direction_cursor_icon(location)
                                        .into(),
                                );
                                cursor_prev_resize_direction = location;
                                return true;
                            }
                        }
                    }
                    winit::event::WindowEvent::MouseInput {
                        state: winit::event::ElementState::Pressed,
                        button: winit::event::MouseButton::Left,
                        ..
                    } => {
                        if let Some(direction) = cursor_prev_resize_direction {
                            let _res = window.drag_resize_window(direction);
                            return true;
                        }
                    }
                    _ => (),
                }

                false
            },
        ))
    } else {
        None
    }
}

/// Get the cursor icon that corresponds to the resize direction.
fn resize_direction_cursor_icon(
    resize_direction: Option<ResizeDirection>,
) -> CursorIcon {
    match resize_direction {
        Some(resize_direction) => match resize_direction {
            ResizeDirection::East => CursorIcon::EResize,
            ResizeDirection::North => CursorIcon::NResize,
            ResizeDirection::NorthEast => CursorIcon::NeResize,
            ResizeDirection::NorthWest => CursorIcon::NwResize,
            ResizeDirection::South => CursorIcon::SResize,
            ResizeDirection::SouthEast => CursorIcon::SeResize,
            ResizeDirection::SouthWest => CursorIcon::SwResize,
            ResizeDirection::West => CursorIcon::WResize,
        },
        None => CursorIcon::Default,
    }
}

/// Identifies resize direction based on cursor position and window dimensions.
#[allow(clippy::similar_names)]
fn cursor_resize_direction(
    win_size: winit::dpi::PhysicalSize<u32>,
    position: winit::dpi::PhysicalPosition<f64>,
    border_size: f64,
) -> Option<ResizeDirection> {
    enum XDirection {
        West,
        East,
        Default,
    }

    enum YDirection {
        North,
        South,
        Default,
    }

    let xdir = if position.x < border_size {
        XDirection::West
    } else if position.x > (win_size.width as f64 - border_size) {
        XDirection::East
    } else {
        XDirection::Default
    };

    let ydir = if position.y < border_size {
        YDirection::North
    } else if position.y > (win_size.height as f64 - border_size) {
        YDirection::South
    } else {
        YDirection::Default
    };

    Some(match xdir {
        XDirection::West => match ydir {
            YDirection::North => ResizeDirection::NorthWest,
            YDirection::South => ResizeDirection::SouthWest,
            YDirection::Default => ResizeDirection::West,
        },

        XDirection::East => match ydir {
            YDirection::North => ResizeDirection::NorthEast,
            YDirection::South => ResizeDirection::SouthEast,
            YDirection::Default => ResizeDirection::East,
        },

        XDirection::Default => match ydir {
            YDirection::North => ResizeDirection::North,
            YDirection::South => ResizeDirection::South,
            YDirection::Default => return None,
        },
    })
}