Struct Application

Source
pub struct Application { /* private fields */ }
Expand description

The top level application object.

This can be thought of as a reference and it can be safely cloned.

Implementations§

Source§

impl Application

Source

pub fn new() -> Result<Application, Error>

Create a new Application.

§Errors

Errors if an Application has already been created.

This may change in the future. See druid#771 for discussion.

Examples found in repository?
examples/edit_text.rs (line 361)
360fn main() {
361    let app = Application::new().unwrap();
362    let mut builder = WindowBuilder::new(app.clone());
363    builder.set_handler(Box::<AppState>::default());
364    builder.set_title("Text editing example");
365    let window = builder.build().unwrap();
366    window.show();
367    app.run(None);
368}
More examples
Hide additional examples
examples/perftest.rs (line 140)
138fn main() {
139    tracing_subscriber::fmt().init();
140    let app = Application::new().unwrap();
141    let mut builder = WindowBuilder::new(app.clone());
142    let perf_test = PerfTest {
143        size: Size::ZERO,
144        handle: Default::default(),
145        start_time: time::Instant::now(),
146        last_time: time::Instant::now(),
147        red: true,
148    };
149    builder.set_handler(Box::new(perf_test));
150    builder.set_title("Performance tester");
151
152    let window = builder.build().unwrap();
153    window.show();
154
155    app.run(None);
156}
examples/invalidate.rs (line 106)
104fn main() {
105    tracing_subscriber::fmt().init();
106    let app = Application::new().unwrap();
107    let mut builder = WindowBuilder::new(app.clone());
108    let inv_test = InvalidateTest {
109        size: Size::ZERO,
110        handle: Default::default(),
111        start_time: Instant::now(),
112        rect: Rect::from_origin_size(Point::ZERO, (10.0, 20.0)),
113        cursor: Rect::from_origin_size(Point::ZERO, (2.0, 100.0)),
114        color: Color::WHITE,
115    };
116    builder.set_handler(Box::new(inv_test));
117    builder.set_title("Invalidate tester");
118
119    let window = builder.build().unwrap();
120    window.show();
121    app.run(None);
122}
examples/quit.rs (line 71)
69fn main() {
70    tracing_subscriber::fmt().init();
71    let app = Application::new().unwrap();
72
73    let mut file_menu = Menu::new();
74    file_menu.add_item(
75        0x100,
76        "E&xit",
77        Some(&HotKey::new(SysMods::Cmd, "q")),
78        None,
79        true,
80    );
81
82    let mut menubar = Menu::new();
83    menubar.add_dropdown(file_menu, "Application", true);
84
85    let mut builder = WindowBuilder::new(app.clone());
86    builder.set_handler(Box::<QuitState>::default());
87    builder.set_title("Quit example");
88    builder.set_menu(menubar);
89
90    let window = builder.build().unwrap();
91    window.show();
92
93    app.run(None);
94}
examples/shello.rs (line 164)
136fn main() {
137    tracing_subscriber::fmt().init();
138    let mut file_menu = Menu::new();
139    file_menu.add_item(
140        0x100,
141        "E&xit",
142        Some(&HotKey::new(SysMods::Cmd, "q")),
143        None,
144        true,
145    );
146    file_menu.add_item(
147        0x101,
148        "O&pen",
149        Some(&HotKey::new(SysMods::Cmd, "o")),
150        None,
151        true,
152    );
153    file_menu.add_item(
154        0x102,
155        "S&ave",
156        Some(&HotKey::new(SysMods::Cmd, "s")),
157        None,
158        true,
159    );
160    let mut menubar = Menu::new();
161    menubar.add_dropdown(Menu::new(), "Application", true);
162    menubar.add_dropdown(file_menu, "&File", true);
163
164    let app = Application::new().unwrap();
165    let mut builder = WindowBuilder::new(app.clone());
166    builder.set_handler(Box::<HelloState>::default());
167    builder.set_title("Hello example");
168    builder.set_menu(menubar);
169
170    let window = builder.build().unwrap();
171    window.show();
172
173    app.run(None);
174}
Source

pub fn global() -> Application

Get the current globally active Application.

A globally active Application exists after new is called and until run returns.

§Panics

Panics if there is no globally active Application. For a non-panicking function use try_global.

This function will also panic if called from a non-main thread.

Examples found in repository?
examples/invalidate.rs (line 96)
95    fn destroy(&mut self) {
96        Application::global().quit()
97    }
More examples
Hide additional examples
examples/perftest.rs (line 130)
129    fn destroy(&mut self) {
130        Application::global().quit()
131    }
examples/quit.rs (line 61)
60    fn destroy(&mut self) {
61        Application::global().quit()
62    }
examples/edit_text.rs (line 123)
119    fn command(&mut self, id: u32) {
120        match id {
121            0x100 => {
122                self.handle.close();
123                Application::global().quit()
124            }
125            _ => println!("unexpected id {id}"),
126        }
127    }
128
129    fn key_down(&mut self, event: KeyEvent) -> bool {
130        if event.key == Key::Character("c".to_string()) {
131            // custom hotkey for pressing "c"
132            println!("user pressed c! wow! setting selection to 0");
133
134            // update internal selection state
135            self.document.borrow_mut().selection = Selection::caret(0);
136
137            // notify the OS that we've updated the selection
138            self.handle
139                .update_text_field(self.text_input_token.unwrap(), Event::SelectionChanged);
140
141            // repaint window
142            self.handle.request_anim_frame();
143
144            // return true prevents the keypress event from being handled as text input
145            return true;
146        }
147        false
148    }
149
150    fn acquire_input_lock(
151        &mut self,
152        _token: TextFieldToken,
153        _mutable: bool,
154    ) -> Box<dyn InputHandler> {
155        Box::new(AppInputHandler {
156            state: self.document.clone(),
157            window_size: self.size,
158            window_handle: self.handle.clone(),
159        })
160    }
161
162    fn release_input_lock(&mut self, _token: TextFieldToken) {
163        // no action required; this example is simple enough that this
164        // state is not actually shared.
165    }
166
167    fn size(&mut self, size: Size) {
168        self.size = size;
169    }
170
171    fn request_close(&mut self) {
172        self.handle.close();
173    }
174
175    fn destroy(&mut self) {
176        Application::global().quit()
177    }
examples/shello.rs (line 51)
47    fn command(&mut self, id: u32) {
48        match id {
49            0x100 => {
50                self.handle.close();
51                Application::global().quit()
52            }
53            0x101 => {
54                let options = FileDialogOptions::new().show_hidden().allowed_types(vec![
55                    FileSpec::new("Rust Files", &["rs", "toml"]),
56                    FileSpec::TEXT,
57                    FileSpec::JPG,
58                ]);
59                self.handle.open_file(options);
60            }
61            0x102 => {
62                let options = FileDialogOptions::new().show_hidden().allowed_types(vec![
63                    FileSpec::new("Rust Files", &["rs", "toml"]),
64                    FileSpec::TEXT,
65                    FileSpec::JPG,
66                ]);
67                self.handle.save_as(options);
68            }
69            _ => println!("unexpected id {id}"),
70        }
71    }
72
73    fn open_file(&mut self, _token: FileDialogToken, file_info: Option<FileInfo>) {
74        println!("open file result: {file_info:?}");
75    }
76
77    fn save_as(&mut self, _token: FileDialogToken, file: Option<FileInfo>) {
78        println!("save file result: {file:?}");
79    }
80
81    fn key_down(&mut self, event: KeyEvent) -> bool {
82        println!("keydown: {event:?}");
83        false
84    }
85
86    fn key_up(&mut self, event: KeyEvent) {
87        println!("keyup: {event:?}");
88    }
89
90    fn wheel(&mut self, event: &MouseEvent) {
91        println!("mouse_wheel {event:?}");
92    }
93
94    fn mouse_move(&mut self, event: &MouseEvent) {
95        self.handle.set_cursor(&Cursor::Arrow);
96        println!("mouse_move {event:?}");
97    }
98
99    fn mouse_down(&mut self, event: &MouseEvent) {
100        println!("mouse_down {event:?}");
101    }
102
103    fn mouse_up(&mut self, event: &MouseEvent) {
104        println!("mouse_up {event:?}");
105    }
106
107    fn timer(&mut self, id: TimerToken) {
108        println!("timer fired: {id:?}");
109    }
110
111    fn size(&mut self, size: Size) {
112        self.size = size;
113    }
114
115    fn got_focus(&mut self) {
116        println!("Got focus");
117    }
118
119    fn lost_focus(&mut self) {
120        println!("Lost focus");
121    }
122
123    fn request_close(&mut self) {
124        self.handle.close();
125    }
126
127    fn destroy(&mut self) {
128        Application::global().quit()
129    }
Source

pub fn try_global() -> Option<Application>

Get the current globally active Application.

A globally active Application exists after new is called and until run returns.

§Panics

Panics if called from a non-main thread.

Source

pub fn run(self, handler: Option<Box<dyn AppHandler>>)

Start the Application runloop.

The provided handler will be used to inform of events.

This will consume the Application and block the current thread until the Application has finished executing.

§Panics

Panics if the Application is already running.

Examples found in repository?
examples/edit_text.rs (line 367)
360fn main() {
361    let app = Application::new().unwrap();
362    let mut builder = WindowBuilder::new(app.clone());
363    builder.set_handler(Box::<AppState>::default());
364    builder.set_title("Text editing example");
365    let window = builder.build().unwrap();
366    window.show();
367    app.run(None);
368}
More examples
Hide additional examples
examples/perftest.rs (line 155)
138fn main() {
139    tracing_subscriber::fmt().init();
140    let app = Application::new().unwrap();
141    let mut builder = WindowBuilder::new(app.clone());
142    let perf_test = PerfTest {
143        size: Size::ZERO,
144        handle: Default::default(),
145        start_time: time::Instant::now(),
146        last_time: time::Instant::now(),
147        red: true,
148    };
149    builder.set_handler(Box::new(perf_test));
150    builder.set_title("Performance tester");
151
152    let window = builder.build().unwrap();
153    window.show();
154
155    app.run(None);
156}
examples/invalidate.rs (line 121)
104fn main() {
105    tracing_subscriber::fmt().init();
106    let app = Application::new().unwrap();
107    let mut builder = WindowBuilder::new(app.clone());
108    let inv_test = InvalidateTest {
109        size: Size::ZERO,
110        handle: Default::default(),
111        start_time: Instant::now(),
112        rect: Rect::from_origin_size(Point::ZERO, (10.0, 20.0)),
113        cursor: Rect::from_origin_size(Point::ZERO, (2.0, 100.0)),
114        color: Color::WHITE,
115    };
116    builder.set_handler(Box::new(inv_test));
117    builder.set_title("Invalidate tester");
118
119    let window = builder.build().unwrap();
120    window.show();
121    app.run(None);
122}
examples/quit.rs (line 93)
69fn main() {
70    tracing_subscriber::fmt().init();
71    let app = Application::new().unwrap();
72
73    let mut file_menu = Menu::new();
74    file_menu.add_item(
75        0x100,
76        "E&xit",
77        Some(&HotKey::new(SysMods::Cmd, "q")),
78        None,
79        true,
80    );
81
82    let mut menubar = Menu::new();
83    menubar.add_dropdown(file_menu, "Application", true);
84
85    let mut builder = WindowBuilder::new(app.clone());
86    builder.set_handler(Box::<QuitState>::default());
87    builder.set_title("Quit example");
88    builder.set_menu(menubar);
89
90    let window = builder.build().unwrap();
91    window.show();
92
93    app.run(None);
94}
examples/shello.rs (line 173)
136fn main() {
137    tracing_subscriber::fmt().init();
138    let mut file_menu = Menu::new();
139    file_menu.add_item(
140        0x100,
141        "E&xit",
142        Some(&HotKey::new(SysMods::Cmd, "q")),
143        None,
144        true,
145    );
146    file_menu.add_item(
147        0x101,
148        "O&pen",
149        Some(&HotKey::new(SysMods::Cmd, "o")),
150        None,
151        true,
152    );
153    file_menu.add_item(
154        0x102,
155        "S&ave",
156        Some(&HotKey::new(SysMods::Cmd, "s")),
157        None,
158        true,
159    );
160    let mut menubar = Menu::new();
161    menubar.add_dropdown(Menu::new(), "Application", true);
162    menubar.add_dropdown(file_menu, "&File", true);
163
164    let app = Application::new().unwrap();
165    let mut builder = WindowBuilder::new(app.clone());
166    builder.set_handler(Box::<HelloState>::default());
167    builder.set_title("Hello example");
168    builder.set_menu(menubar);
169
170    let window = builder.build().unwrap();
171    window.show();
172
173    app.run(None);
174}
Source

pub fn quit(&self)

Quit the Application.

This will cause run to return control back to the calling function.

Examples found in repository?
examples/invalidate.rs (line 96)
95    fn destroy(&mut self) {
96        Application::global().quit()
97    }
More examples
Hide additional examples
examples/perftest.rs (line 130)
129    fn destroy(&mut self) {
130        Application::global().quit()
131    }
examples/quit.rs (line 61)
60    fn destroy(&mut self) {
61        Application::global().quit()
62    }
examples/edit_text.rs (line 123)
119    fn command(&mut self, id: u32) {
120        match id {
121            0x100 => {
122                self.handle.close();
123                Application::global().quit()
124            }
125            _ => println!("unexpected id {id}"),
126        }
127    }
128
129    fn key_down(&mut self, event: KeyEvent) -> bool {
130        if event.key == Key::Character("c".to_string()) {
131            // custom hotkey for pressing "c"
132            println!("user pressed c! wow! setting selection to 0");
133
134            // update internal selection state
135            self.document.borrow_mut().selection = Selection::caret(0);
136
137            // notify the OS that we've updated the selection
138            self.handle
139                .update_text_field(self.text_input_token.unwrap(), Event::SelectionChanged);
140
141            // repaint window
142            self.handle.request_anim_frame();
143
144            // return true prevents the keypress event from being handled as text input
145            return true;
146        }
147        false
148    }
149
150    fn acquire_input_lock(
151        &mut self,
152        _token: TextFieldToken,
153        _mutable: bool,
154    ) -> Box<dyn InputHandler> {
155        Box::new(AppInputHandler {
156            state: self.document.clone(),
157            window_size: self.size,
158            window_handle: self.handle.clone(),
159        })
160    }
161
162    fn release_input_lock(&mut self, _token: TextFieldToken) {
163        // no action required; this example is simple enough that this
164        // state is not actually shared.
165    }
166
167    fn size(&mut self, size: Size) {
168        self.size = size;
169    }
170
171    fn request_close(&mut self) {
172        self.handle.close();
173    }
174
175    fn destroy(&mut self) {
176        Application::global().quit()
177    }
examples/shello.rs (line 51)
47    fn command(&mut self, id: u32) {
48        match id {
49            0x100 => {
50                self.handle.close();
51                Application::global().quit()
52            }
53            0x101 => {
54                let options = FileDialogOptions::new().show_hidden().allowed_types(vec![
55                    FileSpec::new("Rust Files", &["rs", "toml"]),
56                    FileSpec::TEXT,
57                    FileSpec::JPG,
58                ]);
59                self.handle.open_file(options);
60            }
61            0x102 => {
62                let options = FileDialogOptions::new().show_hidden().allowed_types(vec![
63                    FileSpec::new("Rust Files", &["rs", "toml"]),
64                    FileSpec::TEXT,
65                    FileSpec::JPG,
66                ]);
67                self.handle.save_as(options);
68            }
69            _ => println!("unexpected id {id}"),
70        }
71    }
72
73    fn open_file(&mut self, _token: FileDialogToken, file_info: Option<FileInfo>) {
74        println!("open file result: {file_info:?}");
75    }
76
77    fn save_as(&mut self, _token: FileDialogToken, file: Option<FileInfo>) {
78        println!("save file result: {file:?}");
79    }
80
81    fn key_down(&mut self, event: KeyEvent) -> bool {
82        println!("keydown: {event:?}");
83        false
84    }
85
86    fn key_up(&mut self, event: KeyEvent) {
87        println!("keyup: {event:?}");
88    }
89
90    fn wheel(&mut self, event: &MouseEvent) {
91        println!("mouse_wheel {event:?}");
92    }
93
94    fn mouse_move(&mut self, event: &MouseEvent) {
95        self.handle.set_cursor(&Cursor::Arrow);
96        println!("mouse_move {event:?}");
97    }
98
99    fn mouse_down(&mut self, event: &MouseEvent) {
100        println!("mouse_down {event:?}");
101    }
102
103    fn mouse_up(&mut self, event: &MouseEvent) {
104        println!("mouse_up {event:?}");
105    }
106
107    fn timer(&mut self, id: TimerToken) {
108        println!("timer fired: {id:?}");
109    }
110
111    fn size(&mut self, size: Size) {
112        self.size = size;
113    }
114
115    fn got_focus(&mut self) {
116        println!("Got focus");
117    }
118
119    fn lost_focus(&mut self) {
120        println!("Lost focus");
121    }
122
123    fn request_close(&mut self) {
124        self.handle.close();
125    }
126
127    fn destroy(&mut self) {
128        Application::global().quit()
129    }
Source

pub fn clipboard(&self) -> Clipboard

Returns a handle to the system clipboard.

Source

pub fn get_locale() -> String

Returns the current locale string.

This should a Unicode language identifier.

Trait Implementations§

Source§

impl Clone for Application

Source§

fn clone(&self) -> Application

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> RoundFrom<T> for T

Source§

fn round_from(x: T) -> T

Performs the conversion.
Source§

impl<T, U> RoundInto<U> for T
where U: RoundFrom<T>,

Source§

fn round_into(self) -> U

Performs the conversion.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more