hello/
hello.rs

1// Copyright 2018 The xi-editor 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
15extern crate druid_win_shell;
16extern crate direct2d;
17
18use std::any::Any;
19use std::cell::RefCell;
20
21use direct2d::math::*;
22use direct2d::RenderTarget;
23use direct2d::brush::SolidColorBrush;
24
25use druid_win_shell::dialog::{FileDialogOptions, FileDialogType};
26use druid_win_shell::menu::Menu;
27use druid_win_shell::paint::PaintCtx;
28use druid_win_shell::win_main;
29use druid_win_shell::window::{MouseEvent, WindowBuilder, WindowHandle, WinHandler};
30
31#[derive(Default)]
32struct HelloState {
33    handle: RefCell<WindowHandle>,
34}
35
36impl WinHandler for HelloState {
37    fn connect(&self, handle: &WindowHandle) {
38        *self.handle.borrow_mut() = handle.clone();
39    }
40
41    fn paint(&self, paint_ctx: &mut PaintCtx) -> bool {
42        let rt = paint_ctx.render_target();
43        let size = rt.get_size();
44        let rect = RectF::from((0.0, 0.0, size.width, size.height));
45        let bg = SolidColorBrush::create(rt).with_color(0x272822).build().unwrap();
46        let fg = SolidColorBrush::create(rt).with_color(0xf0f0ea).build().unwrap();
47        rt.fill_rectangle(rect, &bg);
48        rt.draw_line((10.0, 50.0), (90.0, 90.0),
49                &fg, 1.0, None);
50        false
51    }
52
53    fn command(&self, id: u32) {
54        match id {
55            0x100 => self.handle.borrow().close(),
56            0x101 => {
57                let mut options = FileDialogOptions::default();
58                options.set_show_hidden();
59                let filename = self.handle.borrow().file_dialog(FileDialogType::Open, options);
60                println!("result: {:?}", filename);
61            }
62            _ => println!("unexpected id {}", id),
63        }
64    }
65
66    fn char(&self, ch: u32, mods: u32) {
67        println!("got char 0x{:x} {:02x}", ch, mods);
68    }
69
70    fn keydown(&self, vk_code: i32, mods: u32) -> bool {
71        println!("got key code 0x{:x} {:02x}", vk_code, mods);
72        false
73    }
74
75    fn mouse_wheel(&self, delta: i32, mods: u32) {
76        println!("mouse_wheel {} {:02x}", delta, mods);
77    }
78
79    fn mouse_hwheel(&self, delta: i32, mods: u32) {
80        println!("mouse_hwheel {} {:02x}", delta, mods);
81    }
82
83    fn mouse_move(&self, x: i32, y: i32, mods: u32) {
84        println!("mouse_move ({}, {}) {:02x}", x, y, mods);
85    }
86
87    fn mouse(&self, event: &MouseEvent) {
88        println!("mouse {:?}", event);
89    }
90
91    fn destroy(&self) {
92        win_main::request_quit();
93    }
94
95    fn as_any(&self) -> &Any { self }
96}
97
98fn main() {
99    druid_win_shell::init();
100
101    let mut file_menu = Menu::new();
102    file_menu.add_item(0x100, "E&xit");
103    file_menu.add_item(0x101, "O&pen");
104    let mut menubar = Menu::new();
105    menubar.add_dropdown(file_menu, "&File");
106
107    let mut run_loop = win_main::RunLoop::new();
108    let mut builder = WindowBuilder::new();
109    builder.set_handler(Box::new(HelloState::default()));
110    builder.set_title("Hello example");
111    builder.set_menu(menubar);
112    let window = builder.build().unwrap();
113    window.show();
114    run_loop.run();
115}