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
// Copyright 2023 The Druid Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A demo of a few window features, including input region, always on top,
//! and titlebar visibility.
//! The demo is setup so that there are parts of the window that you can click through.
//! There are also buttons for setting always on top and setting the visibility of the
//! titlebar.
//! The window's region is managed by the root custom widget.

use druid::widget::prelude::*;
use druid::widget::{Button, Container, Flex, Label, LineBreaking, Widget};
use druid::{AppLauncher, Color, Lens, Point, Rect, Region, WidgetExt, WidgetPod, WindowDesc};

const EXAMPLE_BORDER_SIZE: f64 = 3.0;
const INFO_TEXT: &str = "Only this text and the borders can be interacted with.
You can click through the other parts

This demo is useful for observing the limitations of each OS.
- Windows is well supported. Observation: When the titlebar is enabled and the input region is set, the border becomes invisible. Always on top is supported.
- macOS has good support, but doesn't allow toggling titlebar after the window is opened. Also, it just makes transparent regions transparent automatically when set to have no titlebar. Always on top is supported.
- Linux support varies by desktop environment and display server. Wayland is much more restrictive, with it not allowing things like setting position and always on top. Fortunately desktop environments often allow you to manually set window decoration and always on top on the Window itself. The offsets can differ between desktop environments, and sometimes you need to open the window with the titlebar, then turn it off, for it to work.";

#[derive(Clone, Data, Lens)]
struct AppState {
    limit_input_region: bool,
    show_titlebar: bool,
    always_on_top: bool,
}

struct InputRegionExampleWidget {
    info_label: WidgetPod<AppState, Container<AppState>>,
    controls: WidgetPod<AppState, Flex<AppState>>,
}

impl InputRegionExampleWidget {
    pub fn new() -> Self {
        let info_label = Label::new(INFO_TEXT)
            .with_line_break_mode(LineBreaking::WordWrap)
            .padding(20.0)
            .background(Color::rgba(0.2, 0.2, 0.2, 1.0));
        let toggle_input_region = Button::new("Toggle Input Region")
            .on_click(|ctx, data: &mut bool, _: &Env| {
                *data = !*data;
                tracing::debug!("Setting input region toggle to: {}", *data);
                ctx.request_layout();
            })
            .lens(AppState::limit_input_region);
        let toggle_titlebar = Button::new("Toggle TitleBar")
            .on_click(|ctx, data: &mut bool, _: &Env| {
                *data = !*data;
                tracing::debug!("Setting titlebar visibility to: {}", *data);
                ctx.window().show_titlebar(*data);
                ctx.request_layout();
            })
            .lens(AppState::show_titlebar);
        let toggle_always_on_top = Button::new("Toggle Always On Top")
            .on_click(|ctx, data: &mut bool, _: &Env| {
                *data = !*data;
                tracing::debug!("Setting always on top to: {}", *data);
                ctx.window().set_always_on_top(*data);
            })
            .lens(AppState::always_on_top);
        let controls_flex = Flex::row()
            .with_child(toggle_input_region)
            .with_child(toggle_titlebar)
            .with_child(toggle_always_on_top);
        Self {
            info_label: WidgetPod::new(info_label),
            controls: WidgetPod::new(controls_flex),
        }
    }
}

impl Widget<AppState> for InputRegionExampleWidget {
    fn event(
        &mut self,
        ctx: &mut druid::EventCtx,
        event: &druid::Event,
        data: &mut AppState,
        env: &druid::Env,
    ) {
        self.info_label.event(ctx, event, data, env);
        self.controls.event(ctx, event, data, env);
    }

    fn lifecycle(
        &mut self,
        ctx: &mut druid::LifeCycleCtx,
        event: &druid::LifeCycle,
        data: &AppState,
        env: &druid::Env,
    ) {
        self.info_label.lifecycle(ctx, event, data, env);
        self.controls.lifecycle(ctx, event, data, env);
    }

    fn update(
        &mut self,
        ctx: &mut druid::UpdateCtx,
        _old_data: &AppState,
        data: &AppState,
        env: &druid::Env,
    ) {
        self.info_label.update(ctx, data, env);
        self.controls.update(ctx, data, env);
    }

    fn layout(
        &mut self,
        ctx: &mut druid::LayoutCtx,
        bc: &druid::BoxConstraints,
        data: &AppState,
        env: &druid::Env,
    ) -> druid::Size {
        let mut interactable_area = Region::EMPTY;
        let smaller_bc = BoxConstraints::new(
            Size::new(0.0, 0.0),
            Size::new(bc.max().width - 100.0, bc.max().height - 100.0),
        );
        let full_bc = BoxConstraints::new(Size::new(0.0, 0.0), bc.max());
        let _label_size = self.info_label.layout(ctx, &smaller_bc, data, env);
        let controls_size = self.controls.layout(ctx, &full_bc, data, env);

        let text_origin_point = Point::new(50.0, 50.0 + controls_size.height);
        self.info_label.set_origin(ctx, text_origin_point);
        let controls_origin_point = Point::new(EXAMPLE_BORDER_SIZE, EXAMPLE_BORDER_SIZE);
        self.controls.set_origin(ctx, controls_origin_point);

        // Add side rects to clarify the dimensions of the window.
        let left_rect = Rect::new(0.0, 0.0, EXAMPLE_BORDER_SIZE, bc.max().height);
        let right_rect = Rect::new(
            bc.max().width - EXAMPLE_BORDER_SIZE,
            0.0,
            bc.max().width,
            bc.max().height,
        );
        let bottom_rect = Rect::new(
            0.0,
            bc.max().height - EXAMPLE_BORDER_SIZE,
            bc.max().width,
            bc.max().height,
        );
        interactable_area.add_rect(left_rect);
        interactable_area.add_rect(right_rect);
        interactable_area.add_rect(bottom_rect);
        interactable_area.add_rect(self.info_label.layout_rect());
        interactable_area.add_rect(self.controls.layout_rect());

        if data.limit_input_region {
            ctx.window().set_input_region(Some(interactable_area));
        } else {
            ctx.window().set_input_region(None);
        }

        bc.max()
    }

    fn paint(&mut self, ctx: &mut druid::PaintCtx, data: &AppState, env: &druid::Env) {
        let window_area = ctx.size();
        let left_rect = Rect::new(0.0, 0.0, EXAMPLE_BORDER_SIZE, window_area.height);
        let right_rect = Rect::new(
            window_area.width - EXAMPLE_BORDER_SIZE,
            0.0,
            window_area.width,
            window_area.height,
        );
        let bottom_rect = Rect::new(
            0.0,
            window_area.height - EXAMPLE_BORDER_SIZE,
            window_area.width,
            window_area.height,
        );

        ctx.fill(left_rect, &Color::rgba(1.0, 0., 0., 0.7));
        ctx.fill(right_rect, &Color::rgba(1.0, 0., 0., 0.7));
        ctx.fill(bottom_rect, &Color::rgba(1.0, 0., 0., 0.7));
        self.info_label.paint(ctx, data, env);
        self.controls.paint(ctx, data, env);
    }
}

fn main() {
    let main_window = WindowDesc::new(InputRegionExampleWidget::new())
        .title("Input Region Demo")
        .window_size((750.0, 500.0))
        .with_min_size((650.0, 450.0))
        // Disable the titlebar since it breaks the desired effect on mac.
        // It can be turned on with the button, but not on mac.
        // A lot of apps that will use the interaction features will turn this off
        // On Windows, if on, this will be invisible, but still there.
        .show_titlebar(false)
        .transparent(true);

    let state = AppState {
        limit_input_region: true,
        always_on_top: false,
        show_titlebar: false,
    };

    AppLauncher::with_window(main_window)
        .log_to_console()
        .launch(state)
        .expect("Failed to launch application");
}