transparency/
transparency.rs

1// Copyright 2019 The Druid Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! An example of a transparent window background.
16//! Useful for dropdowns, tooltips and other overlay windows.
17
18// On Windows platform, don't show a console when opening the app.
19#![windows_subsystem = "windows"]
20
21use druid::widget::prelude::*;
22use druid::widget::{Flex, Label, Painter, TextBox, WidgetExt};
23use druid::{kurbo::Circle, widget::Controller};
24use druid::{AppLauncher, Color, Lens, Rect, WindowDesc};
25
26#[derive(Clone, Data, Lens)]
27struct HelloState {
28    name: String,
29}
30
31struct DragController;
32
33impl<T, W: Widget<T>> Controller<T, W> for DragController {
34    fn event(
35        &mut self,
36        _child: &mut W,
37        ctx: &mut EventCtx,
38        event: &Event,
39        _data: &mut T,
40        _env: &Env,
41    ) {
42        if let Event::MouseMove(_) = event {
43            ctx.window().handle_titlebar(true);
44        }
45    }
46}
47
48pub fn main() {
49    let window = WindowDesc::new(build_root_widget())
50        .show_titlebar(false)
51        .window_size((512., 512.))
52        .transparent(true)
53        .resizable(true)
54        .title("Transparent background");
55
56    AppLauncher::with_window(window)
57        .log_to_console()
58        .launch(HelloState { name: "".into() })
59        .expect("launch failed");
60}
61
62fn build_root_widget() -> impl Widget<HelloState> {
63    // Draw red circle, and two semi-transparent rectangles
64    let circle_and_rects = Painter::new(|ctx, _data, _env| {
65        let boundaries = ctx.size().to_rect();
66        let center = (boundaries.width() / 2., boundaries.height() / 2.);
67        let circle = Circle::new(center, center.0.min(center.1));
68        ctx.fill(circle, &Color::RED);
69
70        let rect1 = Rect::new(0., 0., boundaries.width() / 2., boundaries.height() / 2.);
71        ctx.fill(rect1, &Color::rgba8(0x0, 0xff, 0, 125));
72
73        let rect2 = Rect::new(
74            boundaries.width() / 2.,
75            boundaries.height() / 2.,
76            boundaries.width(),
77            boundaries.height(),
78        );
79        ctx.fill(rect2, &Color::rgba8(0x0, 0x0, 0xff, 125));
80    });
81
82    // This textbox modifies the label, idea here is to test that the background
83    // invalidation works when you type to the textbox
84    let textbox = TextBox::new()
85        .with_placeholder("Type to test clearing")
86        .with_text_size(18.0)
87        .lens(HelloState::name)
88        .fix_width(250.);
89
90    let label = Label::new(|data: &HelloState, _env: &Env| {
91        if data.name.is_empty() {
92            "Text: ".to_string()
93        } else {
94            format!("Text: {}!", data.name)
95        }
96    })
97    .with_text_color(Color::RED)
98    .with_text_size(32.0);
99
100    Flex::column()
101        .with_flex_child(circle_and_rects.expand().controller(DragController), 10.0)
102        .with_spacer(4.0)
103        .with_child(textbox)
104        .with_spacer(4.0)
105        .with_child(label)
106}