z_stack/z_stack.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//! A simple test of overlapping widgets.
16
17// On Windows platform, don't show a console when opening the app.
18#![windows_subsystem = "windows"]
19
20use druid::widget::prelude::*;
21use druid::widget::{Button, Label, ZStack};
22use druid::{AppLauncher, Data, Lens, UnitPoint, Vec2, WindowDesc};
23
24#[derive(Clone, Data, Lens)]
25struct State {
26 counter: usize,
27}
28
29pub fn main() {
30 // describe the main window
31 let main_window = WindowDesc::new(build_root_widget())
32 .title("Hello World!")
33 .window_size((400.0, 400.0));
34
35 // create the initial app state
36 let initial_state: State = State { counter: 0 };
37
38 // start the application. Here we pass in the application state.
39 AppLauncher::with_window(main_window)
40 .log_to_console()
41 .launch(initial_state)
42 .expect("Failed to launch application");
43}
44
45fn build_root_widget() -> impl Widget<State> {
46 ZStack::new(
47 Button::from_label(Label::dynamic(|state: &State, _| {
48 format!(
49 "Very large button with text! Count up (currently {})",
50 state.counter
51 )
52 }))
53 .on_click(|_, state: &mut State, _| state.counter += 1),
54 )
55 .with_child(
56 Button::new("Reset").on_click(|_, state: &mut State, _| state.counter = 0),
57 Vec2::new(1.0, 1.0),
58 Vec2::ZERO,
59 UnitPoint::LEFT,
60 Vec2::new(10.0, 0.0),
61 )
62}