shapedwindow_taskbar/
shapedwindow_taskbar.rs

1use fltk::{prelude::*, *};
2
3/*
4   Event handling for win and dock_win can be changed.
5
6   In fn prep_shape(), if you want a different shape as your background e.g a pie (circle),
7   change draw::draw_rounded_rectf(0, 0, w, h, 16) to your desired background e.g draw::pie
8                                   x, y, w, h, r
9   * DO NOT CHANGE ANYTHING ELSE IN THE prep_shape() FUNCTION *
10
11   .center_screen(); -- Not a requirement
12   .with_size(1, 1); -- If you use .center_screen(), (x, y) MUST be >0 otherwise a panic!() will be thrown
13   win.set_color(Color::from_rgb(25, 26, 55)); -- Can be refactored to any color, no requirement e.g enums::Color::White
14   button::Button::default(); -- Not a requirement, remove if need be and import your own Widgets
15*/
16
17fn main() {
18    let app = app::App::default();
19
20    // Act as the application in the taskbar (scroll to event handling)
21    let mut dock_win = window::Window::default()
22        .with_size(1, 1) // So we can place it at the center of the screen (needs a size >0 to be centered)
23        .with_label("TestApplication");
24    dock_win.set_center_screen();
25    dock_win.size_range(0, 0, 0, 0);
26    dock_win.make_resizable(false);
27
28    dock_win.show();
29    dock_win.end();
30
31    let mut win = window::Window::default()
32        .with_size(900, 500)
33        .with_label("TestApplication");
34    win.set_center_screen();
35    win.set_color(enums::Color::from_rgb(26, 25, 55));
36
37    let mut but = button::Button::default()
38        .with_label("Button")
39        .with_size(80, 80)
40        .center_of_parent();
41    but.set_frame(enums::FrameType::OFlatBox);
42    but.set_color(enums::Color::Cyan);
43    but.clear_visible_focus();
44    but.set_callback(|_| println!("Clicked"));
45
46    win.show();
47    win.end();
48
49    let win_shape = prep_shape(win.w(), win.h());
50
51    // Called after showing window
52    win.set_shape(Some(win_shape));
53
54    win.handle({
55        let mut x = 0;
56        let mut y = 0;
57        let mut dock_win = dock_win.clone();
58        move |wself, event| match event {
59            enums::Event::Push => {
60                let coords = app::event_coords();
61                x = coords.0;
62                y = coords.1;
63
64                true
65            }
66            enums::Event::Drag => {
67                wself.resize(
68                    app::event_x_root() - x,
69                    app::event_y_root() - y,
70                    wself.w(),
71                    wself.h(),
72                );
73
74                // Changing dock window position so it's close enough to the center of the application (not "visible" to user)
75                dock_win.resize(
76                    wself.x() + (wself.w() / 2),
77                    wself.y() + (wself.w() / 2),
78                    dock_win.w(),
79                    dock_win.h(),
80                );
81
82                true
83            }
84            enums::Event::Close => {
85                app.quit();
86
87                true
88            }
89            enums::Event::Hide => {
90                app.quit();
91
92                true
93            }
94            _ => false,
95        }
96    });
97
98    // Make main window appear when "opened" via Alt+Tab or Taskbar
99    dock_win.handle({
100        let mut win = win.clone();
101        move |_wself, event| match event {
102            enums::Event::Focus => {
103                let win_shape = prep_shape(win.w(), win.h());
104
105                win.show();
106                win.set_shape(Some(win_shape));
107
108                true
109            }
110            enums::Event::Hide => {
111                win.hide();
112
113                true
114            }
115            enums::Event::Close => {
116                app.quit();
117
118                true
119            }
120            _ => false,
121        }
122    });
123
124    app.run().unwrap();
125}
126
127fn prep_shape(w: i32, h: i32) -> image::RgbImage {
128    let surf = surface::ImageSurface::new(w, h, false);
129
130    surface::ImageSurface::push_current(&surf);
131
132    draw::set_draw_color(enums::Color::Black);
133    draw::draw_rectf(-1, -1, w + 2, h + 2);
134
135    draw::set_draw_color(enums::Color::White);
136    draw::draw_rounded_rectf(0, 0, w, h, 16);
137
138    let img = surf.image().unwrap();
139
140    surface::ImageSurface::pop_current();
141
142    img
143}