Introduce daemon API and unify shell runtimes
This commit is contained in:
parent
368b15f708
commit
341c9a3c12
54 changed files with 1352 additions and 2677 deletions
|
|
@ -66,7 +66,7 @@ iced_core.workspace = true
|
||||||
iced_futures.workspace = true
|
iced_futures.workspace = true
|
||||||
iced_renderer.workspace = true
|
iced_renderer.workspace = true
|
||||||
iced_widget.workspace = true
|
iced_widget.workspace = true
|
||||||
iced_winit.features = ["application"]
|
iced_winit.features = ["program"]
|
||||||
iced_winit.workspace = true
|
iced_winit.workspace = true
|
||||||
|
|
||||||
iced_highlighter.workspace = true
|
iced_highlighter.workspace = true
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use iced::widget::canvas::{
|
||||||
use iced::{Element, Length, Point, Rectangle, Renderer, Subscription, Theme};
|
use iced::{Element, Length, Point, Rectangle, Renderer, Subscription, Theme};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Arc - Iced", Arc::update, Arc::view)
|
iced::application("Arc - Iced", Arc::update, Arc::view)
|
||||||
.subscription(Arc::subscription)
|
.subscription(Arc::subscription)
|
||||||
.theme(|_| Theme::Dark)
|
.theme(|_| Theme::Dark)
|
||||||
.antialiasing(true)
|
.antialiasing(true)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use iced::widget::{button, container, horizontal_space, hover};
|
||||||
use iced::{Element, Length, Theme};
|
use iced::{Element, Length, Theme};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Bezier Tool - Iced", Example::update, Example::view)
|
iced::application("Bezier Tool - Iced", Example::update, Example::view)
|
||||||
.theme(|_| Theme::CatppuccinMocha)
|
.theme(|_| Theme::CatppuccinMocha)
|
||||||
.antialiasing(true)
|
.antialiasing(true)
|
||||||
.run()
|
.run()
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use iced::{Element, Font};
|
||||||
const ICON_FONT: Font = Font::with_name("icons");
|
const ICON_FONT: Font = Font::with_name("icons");
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Checkbox - Iced", Example::update, Example::view)
|
iced::application("Checkbox - Iced", Example::update, Example::view)
|
||||||
.font(include_bytes!("../fonts/icons.ttf").as_slice())
|
.font(include_bytes!("../fonts/icons.ttf").as_slice())
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use iced::{
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
iced::program("Clock - Iced", Clock::update, Clock::view)
|
iced::application("Clock - Iced", Clock::update, Clock::view)
|
||||||
.subscription(Clock::subscription)
|
.subscription(Clock::subscription)
|
||||||
.theme(Clock::theme)
|
.theme(Clock::theme)
|
||||||
.antialiasing(true)
|
.antialiasing(true)
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use std::marker::PhantomData;
|
||||||
use std::ops::RangeInclusive;
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program(
|
iced::application(
|
||||||
"Color Palette - Iced",
|
"Color Palette - Iced",
|
||||||
ColorPalette::update,
|
ColorPalette::update,
|
||||||
ColorPalette::view,
|
ColorPalette::view,
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,13 @@ use iced::window;
|
||||||
use iced::{Alignment, Color, Element, Length, Subscription};
|
use iced::{Alignment, Color, Element, Length, Subscription};
|
||||||
|
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
iced::program("Custom Shader - Iced", IcedCubes::update, IcedCubes::view)
|
iced::application(
|
||||||
.subscription(IcedCubes::subscription)
|
"Custom Shader - Iced",
|
||||||
.run()
|
IcedCubes::update,
|
||||||
|
IcedCubes::view,
|
||||||
|
)
|
||||||
|
.subscription(IcedCubes::subscription)
|
||||||
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
struct IcedCubes {
|
struct IcedCubes {
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,13 @@ use iced::widget::{button, center, column, progress_bar, text, Column};
|
||||||
use iced::{Alignment, Element, Subscription};
|
use iced::{Alignment, Element, Subscription};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Download Progress - Iced", Example::update, Example::view)
|
iced::application(
|
||||||
.subscription(Example::subscription)
|
"Download Progress - Iced",
|
||||||
.run()
|
Example::update,
|
||||||
|
Example::view,
|
||||||
|
)
|
||||||
|
.subscription(Example::subscription)
|
||||||
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ use std::path::{Path, PathBuf};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Editor - Iced", Editor::update, Editor::view)
|
iced::application("Editor - Iced", Editor::update, Editor::view)
|
||||||
.load(Editor::load)
|
.load(Editor::load)
|
||||||
.subscription(Editor::subscription)
|
.subscription(Editor::subscription)
|
||||||
.theme(Editor::theme)
|
.theme(Editor::theme)
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use iced::window;
|
||||||
use iced::{Alignment, Element, Length, Subscription, Task};
|
use iced::{Alignment, Element, Length, Subscription, Task};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Events - Iced", Events::update, Events::view)
|
iced::application("Events - Iced", Events::update, Events::view)
|
||||||
.subscription(Events::subscription)
|
.subscription(Events::subscription)
|
||||||
.exit_on_close_request(false)
|
.exit_on_close_request(false)
|
||||||
.run()
|
.run()
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use iced::window;
|
||||||
use iced::{Alignment, Element, Task};
|
use iced::{Alignment, Element, Task};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Exit - Iced", Exit::update, Exit::view).run()
|
iced::application("Exit - Iced", Exit::update, Exit::view).run()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ use iced::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Ferris - Iced", Image::update, Image::view)
|
iced::application("Ferris - Iced", Image::update, Image::view)
|
||||||
.subscription(Image::subscription)
|
.subscription(Image::subscription)
|
||||||
.theme(|_| Theme::TokyoNight)
|
.theme(|_| Theme::TokyoNight)
|
||||||
.run()
|
.run()
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,16 @@ use std::time::Duration;
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
iced::program("Game of Life - Iced", GameOfLife::update, GameOfLife::view)
|
iced::application(
|
||||||
.subscription(GameOfLife::subscription)
|
"Game of Life - Iced",
|
||||||
.theme(|_| Theme::Dark)
|
GameOfLife::update,
|
||||||
.antialiasing(true)
|
GameOfLife::view,
|
||||||
.centered()
|
)
|
||||||
.run()
|
.subscription(GameOfLife::subscription)
|
||||||
|
.theme(|_| Theme::Dark)
|
||||||
|
.antialiasing(true)
|
||||||
|
.centered()
|
||||||
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
struct GameOfLife {
|
struct GameOfLife {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
|
use iced::application;
|
||||||
use iced::gradient;
|
use iced::gradient;
|
||||||
use iced::program;
|
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
checkbox, column, container, horizontal_space, row, slider, text,
|
checkbox, column, container, horizontal_space, row, slider, text,
|
||||||
};
|
};
|
||||||
|
|
@ -8,7 +8,7 @@ use iced::{Alignment, Color, Element, Length, Radians, Theme};
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
iced::program("Gradient - Iced", Gradient::update, Gradient::view)
|
iced::application("Gradient - Iced", Gradient::update, Gradient::view)
|
||||||
.style(Gradient::style)
|
.style(Gradient::style)
|
||||||
.transparent(true)
|
.transparent(true)
|
||||||
.run()
|
.run()
|
||||||
|
|
@ -95,11 +95,11 @@ impl Gradient {
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn style(&self, theme: &Theme) -> program::Appearance {
|
fn style(&self, theme: &Theme) -> application::Appearance {
|
||||||
use program::DefaultStyle;
|
use application::DefaultStyle;
|
||||||
|
|
||||||
if self.transparent {
|
if self.transparent {
|
||||||
program::Appearance {
|
application::Appearance {
|
||||||
background_color: Color::TRANSPARENT,
|
background_color: Color::TRANSPARENT,
|
||||||
text_color: theme.palette().text,
|
text_color: theme.palette().text,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use iced::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program(Layout::title, Layout::update, Layout::view)
|
iced::application(Layout::title, Layout::update, Layout::view)
|
||||||
.subscription(Layout::subscription)
|
.subscription(Layout::subscription)
|
||||||
.theme(Layout::theme)
|
.theme(Layout::theme)
|
||||||
.run()
|
.run()
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use circular::Circular;
|
||||||
use linear::Linear;
|
use linear::Linear;
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program(
|
iced::application(
|
||||||
"Loading Spinners - Iced",
|
"Loading Spinners - Iced",
|
||||||
LoadingSpinners::update,
|
LoadingSpinners::update,
|
||||||
LoadingSpinners::view,
|
LoadingSpinners::view,
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use iced::{Alignment, Color, Element, Length, Subscription, Task};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Modal - Iced", App::update, App::view)
|
iced::application("Modal - Iced", App::update, App::view)
|
||||||
.subscription(App::subscription)
|
.subscription(App::subscription)
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,21 @@
|
||||||
use iced::executor;
|
|
||||||
use iced::multi_window::{self, Application};
|
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
button, center, column, container, horizontal_space, scrollable, text,
|
button, center, column, container, horizontal_space, scrollable, text,
|
||||||
text_input,
|
text_input,
|
||||||
};
|
};
|
||||||
use iced::window;
|
use iced::window;
|
||||||
use iced::{
|
use iced::{Alignment, Element, Length, Subscription, Task, Theme, Vector};
|
||||||
Alignment, Element, Length, Settings, Subscription, Task, Theme, Vector,
|
|
||||||
};
|
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
Example::run(Settings::default())
|
iced::daemon(Example::title, Example::update, Example::view)
|
||||||
|
.load(|| {
|
||||||
|
window::open(window::Settings::default()).map(Message::WindowOpened)
|
||||||
|
})
|
||||||
|
.subscription(Example::subscription)
|
||||||
|
.theme(Example::theme)
|
||||||
|
.scale_factor(Example::scale_factor)
|
||||||
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
@ -39,21 +42,7 @@ enum Message {
|
||||||
TitleChanged(window::Id, String),
|
TitleChanged(window::Id, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl multi_window::Application for Example {
|
impl Example {
|
||||||
type Executor = executor::Default;
|
|
||||||
type Message = Message;
|
|
||||||
type Theme = Theme;
|
|
||||||
type Flags = ();
|
|
||||||
|
|
||||||
fn new(_flags: ()) -> (Self, Task<Message>) {
|
|
||||||
(
|
|
||||||
Example {
|
|
||||||
windows: BTreeMap::from([(window::Id::MAIN, Window::new(1))]),
|
|
||||||
},
|
|
||||||
Task::none(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn title(&self, window: window::Id) -> String {
|
fn title(&self, window: window::Id) -> String {
|
||||||
self.windows
|
self.windows
|
||||||
.get(&window)
|
.get(&window)
|
||||||
|
|
@ -97,7 +86,11 @@ impl multi_window::Application for Example {
|
||||||
Message::WindowClosed(id) => {
|
Message::WindowClosed(id) => {
|
||||||
self.windows.remove(&id);
|
self.windows.remove(&id);
|
||||||
|
|
||||||
Task::none()
|
if self.windows.is_empty() {
|
||||||
|
iced::exit()
|
||||||
|
} else {
|
||||||
|
Task::none()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Message::ScaleInputChanged(id, scale) => {
|
Message::ScaleInputChanged(id, scale) => {
|
||||||
if let Some(window) = self.windows.get_mut(&id) {
|
if let Some(window) = self.windows.get_mut(&id) {
|
||||||
|
|
@ -149,7 +142,7 @@ impl multi_window::Application for Example {
|
||||||
.unwrap_or(1.0)
|
.unwrap_or(1.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Self::Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
window::close_events().map(Message::WindowClosed)
|
window::close_events().map(Message::WindowClosed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ use std::collections::HashMap;
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
iced::program("Multitouch - Iced", Multitouch::update, Multitouch::view)
|
iced::application("Multitouch - Iced", Multitouch::update, Multitouch::view)
|
||||||
.antialiasing(true)
|
.antialiasing(true)
|
||||||
.centered()
|
.centered()
|
||||||
.run()
|
.run()
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use iced::widget::{
|
||||||
use iced::{Color, Element, Length, Size, Subscription};
|
use iced::{Color, Element, Length, Size, Subscription};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Pane Grid - Iced", Example::update, Example::view)
|
iced::application("Pane Grid - Iced", Example::update, Example::view)
|
||||||
.subscription(Example::subscription)
|
.subscription(Example::subscription)
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use iced::widget::{self, center, column, image, row, text};
|
||||||
use iced::{Alignment, Element, Length, Task};
|
use iced::{Alignment, Element, Length, Task};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program(Pokedex::title, Pokedex::update, Pokedex::view)
|
iced::application(Pokedex::title, Pokedex::update, Pokedex::view)
|
||||||
.load(Pokedex::search)
|
.load(Pokedex::search)
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use iced::widget::{center, column, pick_list, qr_code, row, text, text_input};
|
||||||
use iced::{Alignment, Element, Theme};
|
use iced::{Alignment, Element, Theme};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program(
|
iced::application(
|
||||||
"QR Code Generator - Iced",
|
"QR Code Generator - Iced",
|
||||||
QRGenerator::update,
|
QRGenerator::update,
|
||||||
QRGenerator::view,
|
QRGenerator::view,
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ use ::image::ColorType;
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
iced::program("Screenshot - Iced", Example::update, Example::view)
|
iced::application("Screenshot - Iced", Example::update, Example::view)
|
||||||
.subscription(Example::subscription)
|
.subscription(Example::subscription)
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use once_cell::sync::Lazy;
|
||||||
static SCROLLABLE_ID: Lazy<scrollable::Id> = Lazy::new(scrollable::Id::unique);
|
static SCROLLABLE_ID: Lazy<scrollable::Id> = Lazy::new(scrollable::Id::unique);
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program(
|
iced::application(
|
||||||
"Scrollable - Iced",
|
"Scrollable - Iced",
|
||||||
ScrollableDemo::update,
|
ScrollableDemo::update,
|
||||||
ScrollableDemo::view,
|
ScrollableDemo::view,
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use rand::Rng;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
iced::program(
|
iced::application(
|
||||||
"Sierpinski Triangle - Iced",
|
"Sierpinski Triangle - Iced",
|
||||||
SierpinskiEmulator::update,
|
SierpinskiEmulator::update,
|
||||||
SierpinskiEmulator::view,
|
SierpinskiEmulator::view,
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ use std::time::Instant;
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
iced::program(
|
iced::application(
|
||||||
"Solar System - Iced",
|
"Solar System - Iced",
|
||||||
SolarSystem::update,
|
SolarSystem::update,
|
||||||
SolarSystem::view,
|
SolarSystem::view,
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use iced::{Alignment, Element, Subscription, Theme};
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Stopwatch - Iced", Stopwatch::update, Stopwatch::view)
|
iced::application("Stopwatch - Iced", Stopwatch::update, Stopwatch::view)
|
||||||
.subscription(Stopwatch::subscription)
|
.subscription(Stopwatch::subscription)
|
||||||
.theme(Stopwatch::theme)
|
.theme(Stopwatch::theme)
|
||||||
.run()
|
.run()
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use iced::widget::{
|
||||||
use iced::{Alignment, Element, Length, Theme};
|
use iced::{Alignment, Element, Length, Theme};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Styling - Iced", Styling::update, Styling::view)
|
iced::application("Styling - Iced", Styling::update, Styling::view)
|
||||||
.theme(Styling::theme)
|
.theme(Styling::theme)
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,12 @@ use iced::widget::{button, center, column, text};
|
||||||
use iced::{system, Element, Task};
|
use iced::{system, Element, Task};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("System Information - Iced", Example::update, Example::view)
|
iced::application(
|
||||||
.run()
|
"System Information - Iced",
|
||||||
|
Example::update,
|
||||||
|
Example::view,
|
||||||
|
)
|
||||||
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use std::cell::RefCell;
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
iced::program("The Matrix - Iced", TheMatrix::update, TheMatrix::view)
|
iced::application("The Matrix - Iced", TheMatrix::update, TheMatrix::view)
|
||||||
.subscription(TheMatrix::subscription)
|
.subscription(TheMatrix::subscription)
|
||||||
.antialiasing(true)
|
.antialiasing(true)
|
||||||
.run()
|
.run()
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ use iced::{Alignment, Element, Length, Subscription, Task};
|
||||||
use toast::{Status, Toast};
|
use toast::{Status, Toast};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Toast - Iced", App::update, App::view)
|
iced::application("Toast - Iced", App::update, App::view)
|
||||||
.subscription(App::subscription)
|
.subscription(App::subscription)
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ pub fn main() -> iced::Result {
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
iced::program(Todos::title, Todos::update, Todos::view)
|
iced::application(Todos::title, Todos::update, Todos::view)
|
||||||
.load(Todos::load)
|
.load(Todos::load)
|
||||||
.subscription(Todos::subscription)
|
.subscription(Todos::subscription)
|
||||||
.font(include_bytes!("../fonts/icons.ttf").as_slice())
|
.font(include_bytes!("../fonts/icons.ttf").as_slice())
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ pub fn main() -> iced::Result {
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
iced::program(Tour::title, Tour::update, Tour::view)
|
iced::application(Tour::title, Tour::update, Tour::view)
|
||||||
.centered()
|
.centered()
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use iced::widget::{center, text};
|
||||||
use iced::{Element, Subscription};
|
use iced::{Element, Subscription};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("URL Handler - Iced", App::update, App::view)
|
iced::application("URL Handler - Iced", App::update, App::view)
|
||||||
.subscription(App::subscription)
|
.subscription(App::subscription)
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use iced::widget::{
|
||||||
use iced::{Element, Length, Point, Rectangle, Renderer, Theme, Vector};
|
use iced::{Element, Length, Point, Rectangle, Renderer, Theme, Vector};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program(
|
iced::application(
|
||||||
"Vectorial Text - Iced",
|
"Vectorial Text - Iced",
|
||||||
VectorialText::update,
|
VectorialText::update,
|
||||||
VectorialText::view,
|
VectorialText::view,
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use iced::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("Visible Bounds - Iced", Example::update, Example::view)
|
iced::application("Visible Bounds - Iced", Example::update, Example::view)
|
||||||
.subscription(Example::subscription)
|
.subscription(Example::subscription)
|
||||||
.theme(|_| Theme::Dark)
|
.theme(|_| Theme::Dark)
|
||||||
.run()
|
.run()
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use iced::{color, Element, Length, Subscription, Task};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("WebSocket - Iced", WebSocket::update, WebSocket::view)
|
iced::application("WebSocket - Iced", WebSocket::update, WebSocket::view)
|
||||||
.load(WebSocket::load)
|
.load(WebSocket::load)
|
||||||
.subscription(WebSocket::subscription)
|
.subscription(WebSocket::subscription)
|
||||||
.run()
|
.run()
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,12 @@ pub enum Action<T> {
|
||||||
|
|
||||||
/// Run a system action.
|
/// Run a system action.
|
||||||
System(system::Action),
|
System(system::Action),
|
||||||
|
|
||||||
|
/// Exits the runtime.
|
||||||
|
///
|
||||||
|
/// This will normally close any application windows and
|
||||||
|
/// terminate the runtime loop.
|
||||||
|
Exit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Action<T> {
|
impl<T> Action<T> {
|
||||||
|
|
@ -88,6 +94,7 @@ impl<T> Action<T> {
|
||||||
Action::Clipboard(action) => Err(Action::Clipboard(action)),
|
Action::Clipboard(action) => Err(Action::Clipboard(action)),
|
||||||
Action::Window(action) => Err(Action::Window(action)),
|
Action::Window(action) => Err(Action::Window(action)),
|
||||||
Action::System(action) => Err(Action::System(action)),
|
Action::System(action) => Err(Action::System(action)),
|
||||||
|
Action::Exit => Err(Action::Exit),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -110,6 +117,15 @@ where
|
||||||
}
|
}
|
||||||
Action::Window(_) => write!(f, "Action::Window"),
|
Action::Window(_) => write!(f, "Action::Window"),
|
||||||
Action::System(action) => write!(f, "Action::System({action:?})"),
|
Action::System(action) => write!(f, "Action::System({action:?})"),
|
||||||
|
Action::Exit => write!(f, "Action::Exit"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a [`Task`] that exits the iced runtime.
|
||||||
|
///
|
||||||
|
/// This will normally close any application windows and
|
||||||
|
/// terminate the runtime loop.
|
||||||
|
pub fn exit<T>() -> Task<T> {
|
||||||
|
Task::effect(Action::Exit)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
//! Leverage advanced concepts like custom widgets.
|
//! Leverage advanced concepts like custom widgets.
|
||||||
pub use crate::application::Application;
|
|
||||||
pub use crate::core::clipboard::{self, Clipboard};
|
pub use crate::core::clipboard::{self, Clipboard};
|
||||||
pub use crate::core::image;
|
pub use crate::core::image;
|
||||||
pub use crate::core::layout::{self, Layout};
|
pub use crate::core::layout::{self, Layout};
|
||||||
|
|
|
||||||
|
|
@ -1,278 +1,426 @@
|
||||||
//! Build interactive cross-platform applications.
|
//! Create and run iced applications step by step.
|
||||||
use crate::core::text;
|
//!
|
||||||
use crate::graphics::compositor;
|
//! # Example
|
||||||
use crate::shell::application;
|
//! ```no_run
|
||||||
use crate::{Element, Executor, Settings, Subscription, Task};
|
//! use iced::widget::{button, column, text, Column};
|
||||||
|
//! use iced::Theme;
|
||||||
|
//!
|
||||||
|
//! pub fn main() -> iced::Result {
|
||||||
|
//! iced::application("A counter", update, view)
|
||||||
|
//! .theme(|_| Theme::Dark)
|
||||||
|
//! .centered()
|
||||||
|
//! .run()
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! #[derive(Debug, Clone)]
|
||||||
|
//! enum Message {
|
||||||
|
//! Increment,
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn update(value: &mut u64, message: Message) {
|
||||||
|
//! match message {
|
||||||
|
//! Message::Increment => *value += 1,
|
||||||
|
//! }
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn view(value: &u64) -> Column<Message> {
|
||||||
|
//! column![
|
||||||
|
//! text(value),
|
||||||
|
//! button("+").on_press(Message::Increment),
|
||||||
|
//! ]
|
||||||
|
//! }
|
||||||
|
//! ```
|
||||||
|
use crate::program::{self, Program};
|
||||||
|
use crate::window;
|
||||||
|
use crate::{Element, Font, Result, Settings, Size, Subscription, Task};
|
||||||
|
|
||||||
pub use application::{Appearance, DefaultStyle};
|
use std::borrow::Cow;
|
||||||
|
|
||||||
/// An interactive cross-platform application.
|
pub use crate::shell::program::{Appearance, DefaultStyle};
|
||||||
///
|
|
||||||
/// This trait is the main entrypoint of Iced. Once implemented, you can run
|
/// Creates an iced [`Application`] given its title, update, and view logic.
|
||||||
/// your GUI application by simply calling [`run`](#method.run).
|
|
||||||
///
|
|
||||||
/// - On native platforms, it will run in its own window.
|
|
||||||
/// - On the web, it will take control of the `<title>` and the `<body>` of the
|
|
||||||
/// document.
|
|
||||||
///
|
|
||||||
/// An [`Application`] can execute asynchronous actions by returning a
|
|
||||||
/// [`Task`] in some of its methods.
|
|
||||||
///
|
|
||||||
/// When using an [`Application`] with the `debug` feature enabled, a debug view
|
|
||||||
/// can be toggled by pressing `F12`.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// [The repository has a bunch of examples] that use the [`Application`] trait:
|
|
||||||
///
|
|
||||||
/// - [`clock`], an application that uses the [`Canvas`] widget to draw a clock
|
|
||||||
/// and its hands to display the current time.
|
|
||||||
/// - [`download_progress`], a basic application that asynchronously downloads
|
|
||||||
/// a dummy file of 100 MB and tracks the download progress.
|
|
||||||
/// - [`events`], a log of native events displayed using a conditional
|
|
||||||
/// [`Subscription`].
|
|
||||||
/// - [`game_of_life`], an interactive version of the [Game of Life], invented
|
|
||||||
/// by [John Horton Conway].
|
|
||||||
/// - [`pokedex`], an application that displays a random Pokédex entry (sprite
|
|
||||||
/// included!) by using the [PokéAPI].
|
|
||||||
/// - [`solar_system`], an animated solar system drawn using the [`Canvas`] widget
|
|
||||||
/// and showcasing how to compose different transforms.
|
|
||||||
/// - [`stopwatch`], a watch with start/stop and reset buttons showcasing how
|
|
||||||
/// to listen to time.
|
|
||||||
/// - [`todos`], a todos tracker inspired by [TodoMVC].
|
|
||||||
///
|
|
||||||
/// [The repository has a bunch of examples]: https://github.com/iced-rs/iced/tree/0.12/examples
|
|
||||||
/// [`clock`]: https://github.com/iced-rs/iced/tree/0.12/examples/clock
|
|
||||||
/// [`download_progress`]: https://github.com/iced-rs/iced/tree/0.12/examples/download_progress
|
|
||||||
/// [`events`]: https://github.com/iced-rs/iced/tree/0.12/examples/events
|
|
||||||
/// [`game_of_life`]: https://github.com/iced-rs/iced/tree/0.12/examples/game_of_life
|
|
||||||
/// [`pokedex`]: https://github.com/iced-rs/iced/tree/0.12/examples/pokedex
|
|
||||||
/// [`solar_system`]: https://github.com/iced-rs/iced/tree/0.12/examples/solar_system
|
|
||||||
/// [`stopwatch`]: https://github.com/iced-rs/iced/tree/0.12/examples/stopwatch
|
|
||||||
/// [`todos`]: https://github.com/iced-rs/iced/tree/0.12/examples/todos
|
|
||||||
/// [`Sandbox`]: crate::Sandbox
|
|
||||||
/// [`Canvas`]: crate::widget::Canvas
|
|
||||||
/// [PokéAPI]: https://pokeapi.co/
|
|
||||||
/// [TodoMVC]: http://todomvc.com/
|
|
||||||
///
|
|
||||||
/// ## A simple "Hello, world!"
|
|
||||||
///
|
|
||||||
/// If you just want to get started, here is a simple [`Application`] that
|
|
||||||
/// says "Hello, world!":
|
|
||||||
///
|
///
|
||||||
|
/// # Example
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use iced::advanced::Application;
|
/// use iced::widget::{button, column, text, Column};
|
||||||
/// use iced::executor;
|
|
||||||
/// use iced::{Task, Element, Settings, Theme, Renderer};
|
|
||||||
///
|
///
|
||||||
/// pub fn main() -> iced::Result {
|
/// pub fn main() -> iced::Result {
|
||||||
/// Hello::run(Settings::default())
|
/// iced::application("A counter", update, view).run()
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// struct Hello;
|
/// #[derive(Debug, Clone)]
|
||||||
|
/// enum Message {
|
||||||
|
/// Increment,
|
||||||
|
/// }
|
||||||
///
|
///
|
||||||
/// impl Application for Hello {
|
/// fn update(value: &mut u64, message: Message) {
|
||||||
/// type Executor = executor::Default;
|
/// match message {
|
||||||
/// type Flags = ();
|
/// Message::Increment => *value += 1,
|
||||||
/// type Message = ();
|
|
||||||
/// type Theme = Theme;
|
|
||||||
/// type Renderer = Renderer;
|
|
||||||
///
|
|
||||||
/// fn new(_flags: ()) -> (Hello, Task<Self::Message>) {
|
|
||||||
/// (Hello, Task::none())
|
|
||||||
/// }
|
/// }
|
||||||
|
/// }
|
||||||
///
|
///
|
||||||
/// fn title(&self) -> String {
|
/// fn view(value: &u64) -> Column<Message> {
|
||||||
/// String::from("A cool application")
|
/// column![
|
||||||
/// }
|
/// text(value),
|
||||||
///
|
/// button("+").on_press(Message::Increment),
|
||||||
/// fn update(&mut self, _message: Self::Message) -> Task<Self::Message> {
|
/// ]
|
||||||
/// Task::none()
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// fn view(&self) -> Element<Self::Message> {
|
|
||||||
/// "Hello, world!".into()
|
|
||||||
/// }
|
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub trait Application: Sized
|
pub fn application<State, Message, Theme, Renderer>(
|
||||||
|
title: impl Title<State>,
|
||||||
|
update: impl Update<State, Message>,
|
||||||
|
view: impl for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
||||||
|
) -> Application<impl Program<State = State, Message = Message, Theme = Theme>>
|
||||||
where
|
where
|
||||||
Self::Theme: DefaultStyle,
|
State: 'static,
|
||||||
|
Message: Send + std::fmt::Debug + 'static,
|
||||||
|
Theme: Default + DefaultStyle,
|
||||||
|
Renderer: program::Renderer,
|
||||||
{
|
{
|
||||||
/// The [`Executor`] that will run commands and subscriptions.
|
use std::marker::PhantomData;
|
||||||
///
|
|
||||||
/// The [default executor] can be a good starting point!
|
|
||||||
///
|
|
||||||
/// [`Executor`]: Self::Executor
|
|
||||||
/// [default executor]: crate::executor::Default
|
|
||||||
type Executor: Executor;
|
|
||||||
|
|
||||||
/// The type of __messages__ your [`Application`] will produce.
|
struct Instance<State, Message, Theme, Renderer, Update, View> {
|
||||||
type Message: std::fmt::Debug + Send;
|
update: Update,
|
||||||
|
view: View,
|
||||||
/// The theme of your [`Application`].
|
_state: PhantomData<State>,
|
||||||
type Theme: Default;
|
_message: PhantomData<Message>,
|
||||||
|
_theme: PhantomData<Theme>,
|
||||||
/// The renderer of your [`Application`].
|
_renderer: PhantomData<Renderer>,
|
||||||
type Renderer: text::Renderer + compositor::Default;
|
|
||||||
|
|
||||||
/// The data needed to initialize your [`Application`].
|
|
||||||
type Flags;
|
|
||||||
|
|
||||||
/// Initializes the [`Application`] with the flags provided to
|
|
||||||
/// [`run`] as part of the [`Settings`].
|
|
||||||
///
|
|
||||||
/// Here is where you should return the initial state of your app.
|
|
||||||
///
|
|
||||||
/// Additionally, you can return a [`Task`] if you need to perform some
|
|
||||||
/// async action in the background on startup. This is useful if you want to
|
|
||||||
/// load state from a file, perform an initial HTTP request, etc.
|
|
||||||
///
|
|
||||||
/// [`run`]: Self::run
|
|
||||||
fn new(flags: Self::Flags) -> (Self, Task<Self::Message>);
|
|
||||||
|
|
||||||
/// Returns the current title of the [`Application`].
|
|
||||||
///
|
|
||||||
/// This title can be dynamic! The runtime will automatically update the
|
|
||||||
/// title of your application when necessary.
|
|
||||||
fn title(&self) -> String;
|
|
||||||
|
|
||||||
/// Handles a __message__ and updates the state of the [`Application`].
|
|
||||||
///
|
|
||||||
/// This is where you define your __update logic__. All the __messages__,
|
|
||||||
/// produced by either user interactions or commands, will be handled by
|
|
||||||
/// this method.
|
|
||||||
///
|
|
||||||
/// Any [`Task`] returned will be executed immediately in the background.
|
|
||||||
fn update(&mut self, message: Self::Message) -> Task<Self::Message>;
|
|
||||||
|
|
||||||
/// Returns the widgets to display in the [`Application`].
|
|
||||||
///
|
|
||||||
/// These widgets can produce __messages__ based on user interaction.
|
|
||||||
fn view(&self) -> Element<'_, Self::Message, Self::Theme, Self::Renderer>;
|
|
||||||
|
|
||||||
/// Returns the current [`Theme`] of the [`Application`].
|
|
||||||
///
|
|
||||||
/// [`Theme`]: Self::Theme
|
|
||||||
fn theme(&self) -> Self::Theme {
|
|
||||||
Self::Theme::default()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the current [`Appearance`] of the [`Application`].
|
impl<State, Message, Theme, Renderer, Update, View> Program
|
||||||
fn style(&self, theme: &Self::Theme) -> Appearance {
|
for Instance<State, Message, Theme, Renderer, Update, View>
|
||||||
theme.default_style()
|
where
|
||||||
|
Message: Send + std::fmt::Debug + 'static,
|
||||||
|
Theme: Default + DefaultStyle,
|
||||||
|
Renderer: program::Renderer,
|
||||||
|
Update: self::Update<State, Message>,
|
||||||
|
View: for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
||||||
|
{
|
||||||
|
type State = State;
|
||||||
|
type Message = Message;
|
||||||
|
type Theme = Theme;
|
||||||
|
type Renderer = Renderer;
|
||||||
|
type Executor = iced_futures::backend::default::Executor;
|
||||||
|
|
||||||
|
fn load(&self) -> Task<Self::Message> {
|
||||||
|
Task::none()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(
|
||||||
|
&self,
|
||||||
|
state: &mut Self::State,
|
||||||
|
message: Self::Message,
|
||||||
|
) -> Task<Self::Message> {
|
||||||
|
self.update.update(state, message).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view<'a>(
|
||||||
|
&self,
|
||||||
|
state: &'a Self::State,
|
||||||
|
_window: window::Id,
|
||||||
|
) -> Element<'a, Self::Message, Self::Theme, Self::Renderer> {
|
||||||
|
self.view.view(state).into()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the event [`Subscription`] for the current state of the
|
Application {
|
||||||
/// application.
|
raw: Instance {
|
||||||
///
|
update,
|
||||||
/// A [`Subscription`] will be kept alive as long as you keep returning it,
|
view,
|
||||||
/// and the __messages__ produced will be handled by
|
_state: PhantomData,
|
||||||
/// [`update`](#tymethod.update).
|
_message: PhantomData,
|
||||||
///
|
_theme: PhantomData,
|
||||||
/// By default, this method returns an empty [`Subscription`].
|
_renderer: PhantomData,
|
||||||
fn subscription(&self) -> Subscription<Self::Message> {
|
},
|
||||||
Subscription::none()
|
settings: Settings::default(),
|
||||||
|
window: window::Settings::default(),
|
||||||
}
|
}
|
||||||
|
.title(title)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the scale factor of the [`Application`].
|
/// The underlying definition and configuration of an iced application.
|
||||||
///
|
///
|
||||||
/// It can be used to dynamically control the size of the UI at runtime
|
/// You can use this API to create and run iced applications
|
||||||
/// (i.e. zooming).
|
/// step by step—without coupling your logic to a trait
|
||||||
///
|
/// or a specific type.
|
||||||
/// For instance, a scale factor of `2.0` will make widgets twice as big,
|
///
|
||||||
/// while a scale factor of `0.5` will shrink them to half their size.
|
/// You can create an [`Application`] with the [`application`] helper.
|
||||||
///
|
#[derive(Debug)]
|
||||||
/// By default, it returns `1.0`.
|
pub struct Application<P: Program> {
|
||||||
fn scale_factor(&self) -> f64 {
|
raw: P,
|
||||||
1.0
|
settings: Settings,
|
||||||
}
|
window: window::Settings,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: Program> Application<P> {
|
||||||
/// Runs the [`Application`].
|
/// Runs the [`Application`].
|
||||||
///
|
///
|
||||||
/// On native platforms, this method will take control of the current thread
|
/// The state of the [`Application`] must implement [`Default`].
|
||||||
/// until the [`Application`] exits.
|
/// If your state does not implement [`Default`], use [`run_with`]
|
||||||
|
/// instead.
|
||||||
///
|
///
|
||||||
/// On the web platform, this method __will NOT return__ unless there is an
|
/// [`run_with`]: Self::run_with
|
||||||
/// [`Error`] during startup.
|
pub fn run(self) -> Result
|
||||||
///
|
|
||||||
/// [`Error`]: crate::Error
|
|
||||||
fn run(settings: Settings<Self::Flags>) -> crate::Result
|
|
||||||
where
|
where
|
||||||
Self: 'static,
|
Self: 'static,
|
||||||
|
P::State: Default,
|
||||||
{
|
{
|
||||||
#[allow(clippy::needless_update)]
|
self.run_with(P::State::default)
|
||||||
let renderer_settings = crate::graphics::Settings {
|
}
|
||||||
default_font: settings.default_font,
|
|
||||||
default_text_size: settings.default_text_size,
|
/// Runs the [`Application`] with a closure that creates the initial state.
|
||||||
antialiasing: if settings.antialiasing {
|
pub fn run_with<I>(self, initialize: I) -> Result
|
||||||
Some(crate::graphics::Antialiasing::MSAAx4)
|
where
|
||||||
} else {
|
Self: 'static,
|
||||||
None
|
I: Fn() -> P::State + Clone + 'static,
|
||||||
|
{
|
||||||
|
self.raw
|
||||||
|
.run_with(self.settings, Some(self.window), initialize)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the [`Settings`] that will be used to run the [`Application`].
|
||||||
|
pub fn settings(self, settings: Settings) -> Self {
|
||||||
|
Self { settings, ..self }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the [`Settings::antialiasing`] of the [`Application`].
|
||||||
|
pub fn antialiasing(self, antialiasing: bool) -> Self {
|
||||||
|
Self {
|
||||||
|
settings: Settings {
|
||||||
|
antialiasing,
|
||||||
|
..self.settings
|
||||||
},
|
},
|
||||||
..crate::graphics::Settings::default()
|
..self
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(crate::shell::application::run::<
|
/// Sets the default [`Font`] of the [`Application`].
|
||||||
Instance<Self>,
|
pub fn default_font(self, default_font: Font) -> Self {
|
||||||
Self::Executor,
|
Self {
|
||||||
<Self::Renderer as compositor::Default>::Compositor,
|
settings: Settings {
|
||||||
>(settings.into(), renderer_settings)?)
|
default_font,
|
||||||
|
..self.settings
|
||||||
|
},
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds a font to the list of fonts that will be loaded at the start of the [`Application`].
|
||||||
|
pub fn font(mut self, font: impl Into<Cow<'static, [u8]>>) -> Self {
|
||||||
|
self.settings.fonts.push(font.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the [`window::Settings::position`] to [`window::Position::Centered`] in the [`Application`].
|
||||||
|
pub fn centered(self) -> Self {
|
||||||
|
Self {
|
||||||
|
window: window::Settings {
|
||||||
|
position: window::Position::Centered,
|
||||||
|
..self.window
|
||||||
|
},
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the [`window::Settings::exit_on_close_request`] of the [`Application`].
|
||||||
|
pub fn exit_on_close_request(self, exit_on_close_request: bool) -> Self {
|
||||||
|
Self {
|
||||||
|
window: window::Settings {
|
||||||
|
exit_on_close_request,
|
||||||
|
..self.window
|
||||||
|
},
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the [`window::Settings::size`] of the [`Application`].
|
||||||
|
pub fn window_size(self, size: impl Into<Size>) -> Self {
|
||||||
|
Self {
|
||||||
|
window: window::Settings {
|
||||||
|
size: size.into(),
|
||||||
|
..self.window
|
||||||
|
},
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the [`window::Settings::transparent`] of the [`Application`].
|
||||||
|
pub fn transparent(self, transparent: bool) -> Self {
|
||||||
|
Self {
|
||||||
|
window: window::Settings {
|
||||||
|
transparent,
|
||||||
|
..self.window
|
||||||
|
},
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the [`Title`] of the [`Application`].
|
||||||
|
pub(crate) fn title(
|
||||||
|
self,
|
||||||
|
title: impl Title<P::State>,
|
||||||
|
) -> Application<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Application {
|
||||||
|
raw: program::with_title(self.raw, move |state, _window| {
|
||||||
|
title.title(state)
|
||||||
|
}),
|
||||||
|
settings: self.settings,
|
||||||
|
window: self.window,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the [`Task`] produced by the closure at startup.
|
||||||
|
pub fn load(
|
||||||
|
self,
|
||||||
|
f: impl Fn() -> Task<P::Message>,
|
||||||
|
) -> Application<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Application {
|
||||||
|
raw: program::with_load(self.raw, f),
|
||||||
|
settings: self.settings,
|
||||||
|
window: self.window,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the subscription logic of the [`Application`].
|
||||||
|
pub fn subscription(
|
||||||
|
self,
|
||||||
|
f: impl Fn(&P::State) -> Subscription<P::Message>,
|
||||||
|
) -> Application<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Application {
|
||||||
|
raw: program::with_subscription(self.raw, f),
|
||||||
|
settings: self.settings,
|
||||||
|
window: self.window,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the theme logic of the [`Application`].
|
||||||
|
pub fn theme(
|
||||||
|
self,
|
||||||
|
f: impl Fn(&P::State) -> P::Theme,
|
||||||
|
) -> Application<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Application {
|
||||||
|
raw: program::with_theme(self.raw, move |state, _window| f(state)),
|
||||||
|
settings: self.settings,
|
||||||
|
window: self.window,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the style logic of the [`Application`].
|
||||||
|
pub fn style(
|
||||||
|
self,
|
||||||
|
f: impl Fn(&P::State, &P::Theme) -> Appearance,
|
||||||
|
) -> Application<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Application {
|
||||||
|
raw: program::with_style(self.raw, f),
|
||||||
|
settings: self.settings,
|
||||||
|
window: self.window,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the scale factor of the [`Application`].
|
||||||
|
pub fn scale_factor(
|
||||||
|
self,
|
||||||
|
f: impl Fn(&P::State) -> f64,
|
||||||
|
) -> Application<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Application {
|
||||||
|
raw: program::with_scale_factor(self.raw, move |state, _window| {
|
||||||
|
f(state)
|
||||||
|
}),
|
||||||
|
settings: self.settings,
|
||||||
|
window: self.window,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Instance<A>(A)
|
/// The title logic of some [`Application`].
|
||||||
where
|
///
|
||||||
A: Application,
|
/// This trait is implemented both for `&static str` and
|
||||||
A::Theme: DefaultStyle;
|
/// any closure `Fn(&State) -> String`.
|
||||||
|
///
|
||||||
|
/// This trait allows the [`application`] builder to take any of them.
|
||||||
|
pub trait Title<State> {
|
||||||
|
/// Produces the title of the [`Application`].
|
||||||
|
fn title(&self, state: &State) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
impl<A> crate::runtime::Program for Instance<A>
|
impl<State> Title<State> for &'static str {
|
||||||
|
fn title(&self, _state: &State) -> String {
|
||||||
|
self.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, State> Title<State> for T
|
||||||
where
|
where
|
||||||
A: Application,
|
T: Fn(&State) -> String,
|
||||||
A::Theme: DefaultStyle,
|
|
||||||
{
|
{
|
||||||
type Message = A::Message;
|
fn title(&self, state: &State) -> String {
|
||||||
type Theme = A::Theme;
|
self(state)
|
||||||
type Renderer = A::Renderer;
|
|
||||||
|
|
||||||
fn update(&mut self, message: Self::Message) -> Task<Self::Message> {
|
|
||||||
self.0.update(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn view(&self) -> Element<'_, Self::Message, Self::Theme, Self::Renderer> {
|
|
||||||
self.0.view()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A> application::Application for Instance<A>
|
/// The update logic of some [`Application`].
|
||||||
|
///
|
||||||
|
/// This trait allows the [`application`] builder to take any closure that
|
||||||
|
/// returns any `Into<Task<Message>>`.
|
||||||
|
pub trait Update<State, Message> {
|
||||||
|
/// Processes the message and updates the state of the [`Application`].
|
||||||
|
fn update(
|
||||||
|
&self,
|
||||||
|
state: &mut State,
|
||||||
|
message: Message,
|
||||||
|
) -> impl Into<Task<Message>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, State, Message, C> Update<State, Message> for T
|
||||||
where
|
where
|
||||||
A: Application,
|
T: Fn(&mut State, Message) -> C,
|
||||||
A::Theme: DefaultStyle,
|
C: Into<Task<Message>>,
|
||||||
{
|
{
|
||||||
type Flags = A::Flags;
|
fn update(
|
||||||
|
&self,
|
||||||
fn new(flags: Self::Flags) -> (Self, Task<A::Message>) {
|
state: &mut State,
|
||||||
let (app, command) = A::new(flags);
|
message: Message,
|
||||||
|
) -> impl Into<Task<Message>> {
|
||||||
(Instance(app), command)
|
self(state, message)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
fn title(&self) -> String {
|
|
||||||
self.0.title()
|
/// The view logic of some [`Application`].
|
||||||
}
|
///
|
||||||
|
/// This trait allows the [`application`] builder to take any closure that
|
||||||
fn theme(&self) -> A::Theme {
|
/// returns any `Into<Element<'_, Message>>`.
|
||||||
self.0.theme()
|
pub trait View<'a, State, Message, Theme, Renderer> {
|
||||||
}
|
/// Produces the widget of the [`Application`].
|
||||||
|
fn view(
|
||||||
fn style(&self, theme: &A::Theme) -> Appearance {
|
&self,
|
||||||
self.0.style(theme)
|
state: &'a State,
|
||||||
}
|
) -> impl Into<Element<'a, Message, Theme, Renderer>>;
|
||||||
|
}
|
||||||
fn subscription(&self) -> Subscription<Self::Message> {
|
|
||||||
self.0.subscription()
|
impl<'a, T, State, Message, Theme, Renderer, Widget>
|
||||||
}
|
View<'a, State, Message, Theme, Renderer> for T
|
||||||
|
where
|
||||||
fn scale_factor(&self) -> f64 {
|
T: Fn(&'a State) -> Widget,
|
||||||
self.0.scale_factor()
|
State: 'static,
|
||||||
|
Widget: Into<Element<'a, Message, Theme, Renderer>>,
|
||||||
|
{
|
||||||
|
fn view(
|
||||||
|
&self,
|
||||||
|
state: &'a State,
|
||||||
|
) -> impl Into<Element<'a, Message, Theme, Renderer>> {
|
||||||
|
self(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
298
src/daemon.rs
Normal file
298
src/daemon.rs
Normal file
|
|
@ -0,0 +1,298 @@
|
||||||
|
//! Create and run daemons that run in the background.
|
||||||
|
use crate::application;
|
||||||
|
use crate::program::{self, Program};
|
||||||
|
use crate::window;
|
||||||
|
use crate::{Element, Font, Result, Settings, Subscription, Task};
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
pub use crate::shell::program::{Appearance, DefaultStyle};
|
||||||
|
|
||||||
|
/// Creates an iced [`Daemon`] given its title, update, and view logic.
|
||||||
|
///
|
||||||
|
/// A [`Daemon`] will not open a window by default, but will run silently
|
||||||
|
/// instead until a [`Task`] from [`window::open`] is returned by its update logic.
|
||||||
|
///
|
||||||
|
/// Furthermore, a [`Daemon`] will not stop running when all its windows are closed.
|
||||||
|
/// In order to completely terminate a [`Daemon`], its process must be interrupted or
|
||||||
|
/// its update logic must produce a [`Task`] from [`exit`].
|
||||||
|
///
|
||||||
|
/// [`exit`]: crate::exit
|
||||||
|
pub fn daemon<State, Message, Theme, Renderer>(
|
||||||
|
title: impl Title<State>,
|
||||||
|
update: impl application::Update<State, Message>,
|
||||||
|
view: impl for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
||||||
|
) -> Daemon<impl Program<State = State, Message = Message, Theme = Theme>>
|
||||||
|
where
|
||||||
|
State: 'static,
|
||||||
|
Message: Send + std::fmt::Debug + 'static,
|
||||||
|
Theme: Default + DefaultStyle,
|
||||||
|
Renderer: program::Renderer,
|
||||||
|
{
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
struct Instance<State, Message, Theme, Renderer, Update, View> {
|
||||||
|
update: Update,
|
||||||
|
view: View,
|
||||||
|
_state: PhantomData<State>,
|
||||||
|
_message: PhantomData<Message>,
|
||||||
|
_theme: PhantomData<Theme>,
|
||||||
|
_renderer: PhantomData<Renderer>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<State, Message, Theme, Renderer, Update, View> Program
|
||||||
|
for Instance<State, Message, Theme, Renderer, Update, View>
|
||||||
|
where
|
||||||
|
Message: Send + std::fmt::Debug + 'static,
|
||||||
|
Theme: Default + DefaultStyle,
|
||||||
|
Renderer: program::Renderer,
|
||||||
|
Update: application::Update<State, Message>,
|
||||||
|
View: for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
||||||
|
{
|
||||||
|
type State = State;
|
||||||
|
type Message = Message;
|
||||||
|
type Theme = Theme;
|
||||||
|
type Renderer = Renderer;
|
||||||
|
type Executor = iced_futures::backend::default::Executor;
|
||||||
|
|
||||||
|
fn load(&self) -> Task<Self::Message> {
|
||||||
|
Task::none()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(
|
||||||
|
&self,
|
||||||
|
state: &mut Self::State,
|
||||||
|
message: Self::Message,
|
||||||
|
) -> Task<Self::Message> {
|
||||||
|
self.update.update(state, message).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view<'a>(
|
||||||
|
&self,
|
||||||
|
state: &'a Self::State,
|
||||||
|
window: window::Id,
|
||||||
|
) -> Element<'a, Self::Message, Self::Theme, Self::Renderer> {
|
||||||
|
self.view.view(state, window).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Daemon {
|
||||||
|
raw: Instance {
|
||||||
|
update,
|
||||||
|
view,
|
||||||
|
_state: PhantomData,
|
||||||
|
_message: PhantomData,
|
||||||
|
_theme: PhantomData,
|
||||||
|
_renderer: PhantomData,
|
||||||
|
},
|
||||||
|
settings: Settings::default(),
|
||||||
|
}
|
||||||
|
.title(title)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The underlying definition and configuration of an iced daemon.
|
||||||
|
///
|
||||||
|
/// You can use this API to create and run iced applications
|
||||||
|
/// step by step—without coupling your logic to a trait
|
||||||
|
/// or a specific type.
|
||||||
|
///
|
||||||
|
/// You can create a [`Daemon`] with the [`daemon`] helper.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Daemon<P: Program> {
|
||||||
|
raw: P,
|
||||||
|
settings: Settings,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: Program> Daemon<P> {
|
||||||
|
/// Runs the [`Daemon`].
|
||||||
|
///
|
||||||
|
/// The state of the [`Daemon`] must implement [`Default`].
|
||||||
|
/// If your state does not implement [`Default`], use [`run_with`]
|
||||||
|
/// instead.
|
||||||
|
///
|
||||||
|
/// [`run_with`]: Self::run_with
|
||||||
|
pub fn run(self) -> Result
|
||||||
|
where
|
||||||
|
Self: 'static,
|
||||||
|
P::State: Default,
|
||||||
|
{
|
||||||
|
self.run_with(P::State::default)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the [`Daemon`] with a closure that creates the initial state.
|
||||||
|
pub fn run_with<I>(self, initialize: I) -> Result
|
||||||
|
where
|
||||||
|
Self: 'static,
|
||||||
|
I: Fn() -> P::State + Clone + 'static,
|
||||||
|
{
|
||||||
|
self.raw.run_with(self.settings, None, initialize)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the [`Settings`] that will be used to run the [`Daemon`].
|
||||||
|
pub fn settings(self, settings: Settings) -> Self {
|
||||||
|
Self { settings, ..self }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the [`Settings::antialiasing`] of the [`Daemon`].
|
||||||
|
pub fn antialiasing(self, antialiasing: bool) -> Self {
|
||||||
|
Self {
|
||||||
|
settings: Settings {
|
||||||
|
antialiasing,
|
||||||
|
..self.settings
|
||||||
|
},
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the default [`Font`] of the [`Daemon`].
|
||||||
|
pub fn default_font(self, default_font: Font) -> Self {
|
||||||
|
Self {
|
||||||
|
settings: Settings {
|
||||||
|
default_font,
|
||||||
|
..self.settings
|
||||||
|
},
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds a font to the list of fonts that will be loaded at the start of the [`Daemon`].
|
||||||
|
pub fn font(mut self, font: impl Into<Cow<'static, [u8]>>) -> Self {
|
||||||
|
self.settings.fonts.push(font.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the [`Title`] of the [`Daemon`].
|
||||||
|
pub(crate) fn title(
|
||||||
|
self,
|
||||||
|
title: impl Title<P::State>,
|
||||||
|
) -> Daemon<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Daemon {
|
||||||
|
raw: program::with_title(self.raw, move |state, window| {
|
||||||
|
title.title(state, window)
|
||||||
|
}),
|
||||||
|
settings: self.settings,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the [`Task`] produced by the closure at startup.
|
||||||
|
pub fn load(
|
||||||
|
self,
|
||||||
|
f: impl Fn() -> Task<P::Message>,
|
||||||
|
) -> Daemon<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Daemon {
|
||||||
|
raw: program::with_load(self.raw, f),
|
||||||
|
settings: self.settings,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the subscription logic of the [`Daemon`].
|
||||||
|
pub fn subscription(
|
||||||
|
self,
|
||||||
|
f: impl Fn(&P::State) -> Subscription<P::Message>,
|
||||||
|
) -> Daemon<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Daemon {
|
||||||
|
raw: program::with_subscription(self.raw, f),
|
||||||
|
settings: self.settings,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the theme logic of the [`Daemon`].
|
||||||
|
pub fn theme(
|
||||||
|
self,
|
||||||
|
f: impl Fn(&P::State, window::Id) -> P::Theme,
|
||||||
|
) -> Daemon<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Daemon {
|
||||||
|
raw: program::with_theme(self.raw, f),
|
||||||
|
settings: self.settings,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the style logic of the [`Daemon`].
|
||||||
|
pub fn style(
|
||||||
|
self,
|
||||||
|
f: impl Fn(&P::State, &P::Theme) -> Appearance,
|
||||||
|
) -> Daemon<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Daemon {
|
||||||
|
raw: program::with_style(self.raw, f),
|
||||||
|
settings: self.settings,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the scale factor of the [`Daemon`].
|
||||||
|
pub fn scale_factor(
|
||||||
|
self,
|
||||||
|
f: impl Fn(&P::State, window::Id) -> f64,
|
||||||
|
) -> Daemon<
|
||||||
|
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Daemon {
|
||||||
|
raw: program::with_scale_factor(self.raw, f),
|
||||||
|
settings: self.settings,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The title logic of some [`Daemon`].
|
||||||
|
///
|
||||||
|
/// This trait is implemented both for `&static str` and
|
||||||
|
/// any closure `Fn(&State, window::Id) -> String`.
|
||||||
|
///
|
||||||
|
/// This trait allows the [`daemon`] builder to take any of them.
|
||||||
|
pub trait Title<State> {
|
||||||
|
/// Produces the title of the [`Daemon`].
|
||||||
|
fn title(&self, state: &State, window: window::Id) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<State> Title<State> for &'static str {
|
||||||
|
fn title(&self, _state: &State, _window: window::Id) -> String {
|
||||||
|
self.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, State> Title<State> for T
|
||||||
|
where
|
||||||
|
T: Fn(&State, window::Id) -> String,
|
||||||
|
{
|
||||||
|
fn title(&self, state: &State, window: window::Id) -> String {
|
||||||
|
self(state, window)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The view logic of some [`Daemon`].
|
||||||
|
///
|
||||||
|
/// This trait allows the [`daemon`] builder to take any closure that
|
||||||
|
/// returns any `Into<Element<'_, Message>>`.
|
||||||
|
pub trait View<'a, State, Message, Theme, Renderer> {
|
||||||
|
/// Produces the widget of the [`Daemon`].
|
||||||
|
fn view(
|
||||||
|
&self,
|
||||||
|
state: &'a State,
|
||||||
|
window: window::Id,
|
||||||
|
) -> impl Into<Element<'a, Message, Theme, Renderer>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T, State, Message, Theme, Renderer, Widget>
|
||||||
|
View<'a, State, Message, Theme, Renderer> for T
|
||||||
|
where
|
||||||
|
T: Fn(&'a State, window::Id) -> Widget,
|
||||||
|
State: 'static,
|
||||||
|
Widget: Into<Element<'a, Message, Theme, Renderer>>,
|
||||||
|
{
|
||||||
|
fn view(
|
||||||
|
&self,
|
||||||
|
state: &'a State,
|
||||||
|
window: window::Id,
|
||||||
|
) -> impl Into<Element<'a, Message, Theme, Renderer>> {
|
||||||
|
self(state, window)
|
||||||
|
}
|
||||||
|
}
|
||||||
33
src/lib.rs
33
src/lib.rs
|
|
@ -158,11 +158,11 @@
|
||||||
//! 1. Draw the resulting user interface.
|
//! 1. Draw the resulting user interface.
|
||||||
//!
|
//!
|
||||||
//! # Usage
|
//! # Usage
|
||||||
//! Use [`run`] or the [`program`] builder.
|
//! Use [`run`] or the [`application`] builder.
|
||||||
//!
|
//!
|
||||||
//! [Elm]: https://elm-lang.org/
|
//! [Elm]: https://elm-lang.org/
|
||||||
//! [The Elm Architecture]: https://guide.elm-lang.org/architecture/
|
//! [The Elm Architecture]: https://guide.elm-lang.org/architecture/
|
||||||
//! [`program`]: program()
|
//! [`application`]: application()
|
||||||
#![doc(
|
#![doc(
|
||||||
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
|
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
|
||||||
)]
|
)]
|
||||||
|
|
@ -179,10 +179,11 @@ pub use iced_futures::futures;
|
||||||
#[cfg(feature = "highlighter")]
|
#[cfg(feature = "highlighter")]
|
||||||
pub use iced_highlighter as highlighter;
|
pub use iced_highlighter as highlighter;
|
||||||
|
|
||||||
mod application;
|
|
||||||
mod error;
|
mod error;
|
||||||
|
mod program;
|
||||||
|
|
||||||
pub mod program;
|
pub mod application;
|
||||||
|
pub mod daemon;
|
||||||
pub mod settings;
|
pub mod settings;
|
||||||
pub mod time;
|
pub mod time;
|
||||||
pub mod window;
|
pub mod window;
|
||||||
|
|
@ -190,9 +191,6 @@ pub mod window;
|
||||||
#[cfg(feature = "advanced")]
|
#[cfg(feature = "advanced")]
|
||||||
pub mod advanced;
|
pub mod advanced;
|
||||||
|
|
||||||
#[cfg(feature = "multi-window")]
|
|
||||||
pub mod multi_window;
|
|
||||||
|
|
||||||
pub use crate::core::alignment;
|
pub use crate::core::alignment;
|
||||||
pub use crate::core::border;
|
pub use crate::core::border;
|
||||||
pub use crate::core::color;
|
pub use crate::core::color;
|
||||||
|
|
@ -203,7 +201,7 @@ pub use crate::core::{
|
||||||
Length, Padding, Pixels, Point, Radians, Rectangle, Rotation, Shadow, Size,
|
Length, Padding, Pixels, Point, Radians, Rectangle, Rotation, Shadow, Size,
|
||||||
Theme, Transformation, Vector,
|
Theme, Transformation, Vector,
|
||||||
};
|
};
|
||||||
pub use crate::runtime::Task;
|
pub use crate::runtime::{exit, Task};
|
||||||
|
|
||||||
pub mod clipboard {
|
pub mod clipboard {
|
||||||
//! Access the clipboard.
|
//! Access the clipboard.
|
||||||
|
|
@ -308,11 +306,12 @@ pub mod widget {
|
||||||
mod runtime {}
|
mod runtime {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub use application::{application, Application};
|
||||||
|
pub use daemon::{daemon, Daemon};
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
pub use event::Event;
|
pub use event::Event;
|
||||||
pub use executor::Executor;
|
pub use executor::Executor;
|
||||||
pub use font::Font;
|
pub use font::Font;
|
||||||
pub use program::Program;
|
|
||||||
pub use renderer::Renderer;
|
pub use renderer::Renderer;
|
||||||
pub use settings::Settings;
|
pub use settings::Settings;
|
||||||
pub use subscription::Subscription;
|
pub use subscription::Subscription;
|
||||||
|
|
@ -327,13 +326,13 @@ pub type Element<
|
||||||
Renderer = crate::Renderer,
|
Renderer = crate::Renderer,
|
||||||
> = crate::core::Element<'a, Message, Theme, Renderer>;
|
> = crate::core::Element<'a, Message, Theme, Renderer>;
|
||||||
|
|
||||||
/// The result of running a [`Program`].
|
/// The result of running an iced program.
|
||||||
pub type Result = std::result::Result<(), Error>;
|
pub type Result = std::result::Result<(), Error>;
|
||||||
|
|
||||||
/// Runs a basic iced application with default [`Settings`] given its title,
|
/// Runs a basic iced application with default [`Settings`] given its title,
|
||||||
/// update, and view logic.
|
/// update, and view logic.
|
||||||
///
|
///
|
||||||
/// This is equivalent to chaining [`program`] with [`Program::run`].
|
/// This is equivalent to chaining [`application()`] with [`Application::run`].
|
||||||
///
|
///
|
||||||
/// [`program`]: program()
|
/// [`program`]: program()
|
||||||
///
|
///
|
||||||
|
|
@ -364,9 +363,10 @@ pub type Result = std::result::Result<(), Error>;
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn run<State, Message, Theme, Renderer>(
|
pub fn run<State, Message, Theme, Renderer>(
|
||||||
title: impl program::Title<State> + 'static,
|
title: impl application::Title<State> + 'static,
|
||||||
update: impl program::Update<State, Message> + 'static,
|
update: impl application::Update<State, Message> + 'static,
|
||||||
view: impl for<'a> program::View<'a, State, Message, Theme, Renderer> + 'static,
|
view: impl for<'a> application::View<'a, State, Message, Theme, Renderer>
|
||||||
|
+ 'static,
|
||||||
) -> Result
|
) -> Result
|
||||||
where
|
where
|
||||||
State: Default + 'static,
|
State: Default + 'static,
|
||||||
|
|
@ -374,8 +374,5 @@ where
|
||||||
Theme: Default + program::DefaultStyle + 'static,
|
Theme: Default + program::DefaultStyle + 'static,
|
||||||
Renderer: program::Renderer + 'static,
|
Renderer: program::Renderer + 'static,
|
||||||
{
|
{
|
||||||
program(title, update, view).run()
|
application(title, update, view).run()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(inline)]
|
|
||||||
pub use program::program;
|
|
||||||
|
|
|
||||||
|
|
@ -1,254 +0,0 @@
|
||||||
//! Leverage multi-window support in your application.
|
|
||||||
use crate::window;
|
|
||||||
use crate::{Element, Executor, Settings, Subscription, Task};
|
|
||||||
|
|
||||||
pub use crate::application::{Appearance, DefaultStyle};
|
|
||||||
|
|
||||||
/// An interactive cross-platform multi-window application.
|
|
||||||
///
|
|
||||||
/// This trait is the main entrypoint of Iced. Once implemented, you can run
|
|
||||||
/// your GUI application by simply calling [`run`](#method.run).
|
|
||||||
///
|
|
||||||
/// - On native platforms, it will run in its own windows.
|
|
||||||
/// - On the web, it will take control of the `<title>` and the `<body>` of the
|
|
||||||
/// document and display only the contents of the `window::Id::MAIN` window.
|
|
||||||
///
|
|
||||||
/// An [`Application`] can execute asynchronous actions by returning a
|
|
||||||
/// [`Task`] in some of its methods.
|
|
||||||
///
|
|
||||||
/// When using an [`Application`] with the `debug` feature enabled, a debug view
|
|
||||||
/// can be toggled by pressing `F12`.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// See the `examples/multi-window` example to see this multi-window `Application` trait in action.
|
|
||||||
///
|
|
||||||
/// ## A simple "Hello, world!"
|
|
||||||
///
|
|
||||||
/// If you just want to get started, here is a simple [`Application`] that
|
|
||||||
/// says "Hello, world!":
|
|
||||||
///
|
|
||||||
/// ```no_run
|
|
||||||
/// use iced::{executor, window};
|
|
||||||
/// use iced::{Task, Element, Settings, Theme};
|
|
||||||
/// use iced::multi_window::{self, Application};
|
|
||||||
///
|
|
||||||
/// pub fn main() -> iced::Result {
|
|
||||||
/// Hello::run(Settings::default())
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// struct Hello;
|
|
||||||
///
|
|
||||||
/// impl multi_window::Application for Hello {
|
|
||||||
/// type Executor = executor::Default;
|
|
||||||
/// type Flags = ();
|
|
||||||
/// type Message = ();
|
|
||||||
/// type Theme = Theme;
|
|
||||||
///
|
|
||||||
/// fn new(_flags: ()) -> (Hello, Task<Self::Message>) {
|
|
||||||
/// (Hello, Task::none())
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// fn title(&self, _window: window::Id) -> String {
|
|
||||||
/// String::from("A cool application")
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// fn update(&mut self, _message: Self::Message) -> Task<Self::Message> {
|
|
||||||
/// Task::none()
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// fn view(&self, _window: window::Id) -> Element<Self::Message> {
|
|
||||||
/// "Hello, world!".into()
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// [`Sandbox`]: crate::Sandbox
|
|
||||||
pub trait Application: Sized
|
|
||||||
where
|
|
||||||
Self::Theme: DefaultStyle,
|
|
||||||
{
|
|
||||||
/// The [`Executor`] that will run commands and subscriptions.
|
|
||||||
///
|
|
||||||
/// The [default executor] can be a good starting point!
|
|
||||||
///
|
|
||||||
/// [`Executor`]: Self::Executor
|
|
||||||
/// [default executor]: crate::executor::Default
|
|
||||||
type Executor: Executor;
|
|
||||||
|
|
||||||
/// The type of __messages__ your [`Application`] will produce.
|
|
||||||
type Message: std::fmt::Debug + Send;
|
|
||||||
|
|
||||||
/// The theme of your [`Application`].
|
|
||||||
type Theme: Default;
|
|
||||||
|
|
||||||
/// The data needed to initialize your [`Application`].
|
|
||||||
type Flags;
|
|
||||||
|
|
||||||
/// Initializes the [`Application`] with the flags provided to
|
|
||||||
/// [`run`] as part of the [`Settings`].
|
|
||||||
///
|
|
||||||
/// Here is where you should return the initial state of your app.
|
|
||||||
///
|
|
||||||
/// Additionally, you can return a [`Task`] if you need to perform some
|
|
||||||
/// async action in the background on startup. This is useful if you want to
|
|
||||||
/// load state from a file, perform an initial HTTP request, etc.
|
|
||||||
///
|
|
||||||
/// [`run`]: Self::run
|
|
||||||
fn new(flags: Self::Flags) -> (Self, Task<Self::Message>);
|
|
||||||
|
|
||||||
/// Returns the current title of the `window` of the [`Application`].
|
|
||||||
///
|
|
||||||
/// This title can be dynamic! The runtime will automatically update the
|
|
||||||
/// title of your window when necessary.
|
|
||||||
fn title(&self, window: window::Id) -> String;
|
|
||||||
|
|
||||||
/// Handles a __message__ and updates the state of the [`Application`].
|
|
||||||
///
|
|
||||||
/// This is where you define your __update logic__. All the __messages__,
|
|
||||||
/// produced by either user interactions or commands, will be handled by
|
|
||||||
/// this method.
|
|
||||||
///
|
|
||||||
/// Any [`Task`] returned will be executed immediately in the background.
|
|
||||||
fn update(&mut self, message: Self::Message) -> Task<Self::Message>;
|
|
||||||
|
|
||||||
/// Returns the widgets to display in the `window` of the [`Application`].
|
|
||||||
///
|
|
||||||
/// These widgets can produce __messages__ based on user interaction.
|
|
||||||
fn view(
|
|
||||||
&self,
|
|
||||||
window: window::Id,
|
|
||||||
) -> Element<'_, Self::Message, Self::Theme, crate::Renderer>;
|
|
||||||
|
|
||||||
/// Returns the current [`Theme`] of the `window` of the [`Application`].
|
|
||||||
///
|
|
||||||
/// [`Theme`]: Self::Theme
|
|
||||||
#[allow(unused_variables)]
|
|
||||||
fn theme(&self, window: window::Id) -> Self::Theme {
|
|
||||||
Self::Theme::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current `Style` of the [`Theme`].
|
|
||||||
///
|
|
||||||
/// [`Theme`]: Self::Theme
|
|
||||||
fn style(&self, theme: &Self::Theme) -> Appearance {
|
|
||||||
Self::Theme::default_style(theme)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the event [`Subscription`] for the current state of the
|
|
||||||
/// application.
|
|
||||||
///
|
|
||||||
/// A [`Subscription`] will be kept alive as long as you keep returning it,
|
|
||||||
/// and the __messages__ produced will be handled by
|
|
||||||
/// [`update`](#tymethod.update).
|
|
||||||
///
|
|
||||||
/// By default, this method returns an empty [`Subscription`].
|
|
||||||
fn subscription(&self) -> Subscription<Self::Message> {
|
|
||||||
Subscription::none()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the scale factor of the `window` of the [`Application`].
|
|
||||||
///
|
|
||||||
/// It can be used to dynamically control the size of the UI at runtime
|
|
||||||
/// (i.e. zooming).
|
|
||||||
///
|
|
||||||
/// For instance, a scale factor of `2.0` will make widgets twice as big,
|
|
||||||
/// while a scale factor of `0.5` will shrink them to half their size.
|
|
||||||
///
|
|
||||||
/// By default, it returns `1.0`.
|
|
||||||
#[allow(unused_variables)]
|
|
||||||
fn scale_factor(&self, window: window::Id) -> f64 {
|
|
||||||
1.0
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Runs the multi-window [`Application`].
|
|
||||||
///
|
|
||||||
/// On native platforms, this method will take control of the current thread
|
|
||||||
/// until the [`Application`] exits.
|
|
||||||
///
|
|
||||||
/// On the web platform, this method __will NOT return__ unless there is an
|
|
||||||
/// [`Error`] during startup.
|
|
||||||
///
|
|
||||||
/// [`Error`]: crate::Error
|
|
||||||
fn run(settings: Settings<Self::Flags>) -> crate::Result
|
|
||||||
where
|
|
||||||
Self: 'static,
|
|
||||||
{
|
|
||||||
#[allow(clippy::needless_update)]
|
|
||||||
let renderer_settings = crate::graphics::Settings {
|
|
||||||
default_font: settings.default_font,
|
|
||||||
default_text_size: settings.default_text_size,
|
|
||||||
antialiasing: if settings.antialiasing {
|
|
||||||
Some(crate::graphics::Antialiasing::MSAAx4)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
},
|
|
||||||
..crate::graphics::Settings::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(crate::shell::multi_window::run::<
|
|
||||||
Instance<Self>,
|
|
||||||
Self::Executor,
|
|
||||||
crate::renderer::Compositor,
|
|
||||||
>(settings.into(), renderer_settings)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Instance<A>(A)
|
|
||||||
where
|
|
||||||
A: Application,
|
|
||||||
A::Theme: DefaultStyle;
|
|
||||||
|
|
||||||
impl<A> crate::runtime::multi_window::Program for Instance<A>
|
|
||||||
where
|
|
||||||
A: Application,
|
|
||||||
A::Theme: DefaultStyle,
|
|
||||||
{
|
|
||||||
type Message = A::Message;
|
|
||||||
type Theme = A::Theme;
|
|
||||||
type Renderer = crate::Renderer;
|
|
||||||
|
|
||||||
fn update(&mut self, message: Self::Message) -> Task<Self::Message> {
|
|
||||||
self.0.update(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn view(
|
|
||||||
&self,
|
|
||||||
window: window::Id,
|
|
||||||
) -> Element<'_, Self::Message, Self::Theme, Self::Renderer> {
|
|
||||||
self.0.view(window)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A> crate::shell::multi_window::Application for Instance<A>
|
|
||||||
where
|
|
||||||
A: Application,
|
|
||||||
A::Theme: DefaultStyle,
|
|
||||||
{
|
|
||||||
type Flags = A::Flags;
|
|
||||||
|
|
||||||
fn new(flags: Self::Flags) -> (Self, Task<A::Message>) {
|
|
||||||
let (app, command) = A::new(flags);
|
|
||||||
|
|
||||||
(Instance(app), command)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn title(&self, window: window::Id) -> String {
|
|
||||||
self.0.title(window)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn theme(&self, window: window::Id) -> A::Theme {
|
|
||||||
self.0.theme(window)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn style(&self, theme: &Self::Theme) -> Appearance {
|
|
||||||
self.0.style(theme)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Self::Message> {
|
|
||||||
self.0.subscription()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scale_factor(&self, window: window::Id) -> f64 {
|
|
||||||
self.0.scale_factor(window)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
830
src/program.rs
830
src/program.rs
File diff suppressed because it is too large
Load diff
|
|
@ -1,30 +1,17 @@
|
||||||
//! Configure your application.
|
//! Configure your application.
|
||||||
use crate::window;
|
|
||||||
use crate::{Font, Pixels};
|
use crate::{Font, Pixels};
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
/// The settings of an iced [`Program`].
|
/// The settings of an iced program.
|
||||||
///
|
|
||||||
/// [`Program`]: crate::Program
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Settings<Flags = ()> {
|
pub struct Settings {
|
||||||
/// The identifier of the application.
|
/// The identifier of the application.
|
||||||
///
|
///
|
||||||
/// If provided, this identifier may be used to identify the application or
|
/// If provided, this identifier may be used to identify the application or
|
||||||
/// communicate with it through the windowing system.
|
/// communicate with it through the windowing system.
|
||||||
pub id: Option<String>,
|
pub id: Option<String>,
|
||||||
|
|
||||||
/// The window settings.
|
|
||||||
///
|
|
||||||
/// They will be ignored on the Web.
|
|
||||||
pub window: window::Settings,
|
|
||||||
|
|
||||||
/// The data needed to initialize the [`Program`].
|
|
||||||
///
|
|
||||||
/// [`Program`]: crate::Program
|
|
||||||
pub flags: Flags,
|
|
||||||
|
|
||||||
/// The fonts to load on boot.
|
/// The fonts to load on boot.
|
||||||
pub fonts: Vec<Cow<'static, [u8]>>,
|
pub fonts: Vec<Cow<'static, [u8]>>,
|
||||||
|
|
||||||
|
|
@ -50,34 +37,10 @@ pub struct Settings<Flags = ()> {
|
||||||
pub antialiasing: bool,
|
pub antialiasing: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Flags> Settings<Flags> {
|
impl Default for Settings {
|
||||||
/// Initialize [`Program`] settings using the given data.
|
|
||||||
///
|
|
||||||
/// [`Program`]: crate::Program
|
|
||||||
pub fn with_flags(flags: Flags) -> Self {
|
|
||||||
let default_settings = Settings::<()>::default();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
flags,
|
|
||||||
id: default_settings.id,
|
|
||||||
window: default_settings.window,
|
|
||||||
fonts: default_settings.fonts,
|
|
||||||
default_font: default_settings.default_font,
|
|
||||||
default_text_size: default_settings.default_text_size,
|
|
||||||
antialiasing: default_settings.antialiasing,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Flags> Default for Settings<Flags>
|
|
||||||
where
|
|
||||||
Flags: Default,
|
|
||||||
{
|
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: None,
|
id: None,
|
||||||
window: window::Settings::default(),
|
|
||||||
flags: Default::default(),
|
|
||||||
fonts: Vec::new(),
|
fonts: Vec::new(),
|
||||||
default_font: Font::default(),
|
default_font: Font::default(),
|
||||||
default_text_size: Pixels(16.0),
|
default_text_size: Pixels(16.0),
|
||||||
|
|
@ -86,12 +49,10 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Flags> From<Settings<Flags>> for iced_winit::Settings<Flags> {
|
impl From<Settings> for iced_winit::Settings {
|
||||||
fn from(settings: Settings<Flags>) -> iced_winit::Settings<Flags> {
|
fn from(settings: Settings) -> iced_winit::Settings {
|
||||||
iced_winit::Settings {
|
iced_winit::Settings {
|
||||||
id: settings.id,
|
id: settings.id,
|
||||||
window: settings.window,
|
|
||||||
flags: settings.flags,
|
|
||||||
fonts: settings.fonts,
|
fonts: settings.fonts,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ workspace = true
|
||||||
default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"]
|
default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"]
|
||||||
debug = ["iced_runtime/debug"]
|
debug = ["iced_runtime/debug"]
|
||||||
system = ["sysinfo"]
|
system = ["sysinfo"]
|
||||||
application = []
|
program = []
|
||||||
x11 = ["winit/x11"]
|
x11 = ["winit/x11"]
|
||||||
wayland = ["winit/wayland"]
|
wayland = ["winit/wayland"]
|
||||||
wayland-dlopen = ["winit/wayland-dlopen"]
|
wayland-dlopen = ["winit/wayland-dlopen"]
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,221 +0,0 @@
|
||||||
use crate::application;
|
|
||||||
use crate::conversion;
|
|
||||||
use crate::core::mouse;
|
|
||||||
use crate::core::{Color, Size};
|
|
||||||
use crate::graphics::Viewport;
|
|
||||||
use crate::runtime::Debug;
|
|
||||||
use crate::Application;
|
|
||||||
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
use winit::event::{Touch, WindowEvent};
|
|
||||||
use winit::window::Window;
|
|
||||||
|
|
||||||
/// The state of a windowed [`Application`].
|
|
||||||
#[allow(missing_debug_implementations)]
|
|
||||||
pub struct State<A: Application>
|
|
||||||
where
|
|
||||||
A::Theme: application::DefaultStyle,
|
|
||||||
{
|
|
||||||
title: String,
|
|
||||||
scale_factor: f64,
|
|
||||||
viewport: Viewport,
|
|
||||||
viewport_version: usize,
|
|
||||||
cursor_position: Option<winit::dpi::PhysicalPosition<f64>>,
|
|
||||||
modifiers: winit::keyboard::ModifiersState,
|
|
||||||
theme: A::Theme,
|
|
||||||
appearance: application::Appearance,
|
|
||||||
application: PhantomData<A>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A: Application> State<A>
|
|
||||||
where
|
|
||||||
A::Theme: application::DefaultStyle,
|
|
||||||
{
|
|
||||||
/// Creates a new [`State`] for the provided [`Application`] and window.
|
|
||||||
pub fn new(application: &A, window: &Window) -> Self {
|
|
||||||
let title = application.title();
|
|
||||||
let scale_factor = application.scale_factor();
|
|
||||||
let theme = application.theme();
|
|
||||||
let appearance = application.style(&theme);
|
|
||||||
|
|
||||||
let viewport = {
|
|
||||||
let physical_size = window.inner_size();
|
|
||||||
|
|
||||||
Viewport::with_physical_size(
|
|
||||||
Size::new(physical_size.width, physical_size.height),
|
|
||||||
window.scale_factor() * scale_factor,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
Self {
|
|
||||||
title,
|
|
||||||
scale_factor,
|
|
||||||
viewport,
|
|
||||||
viewport_version: 0,
|
|
||||||
cursor_position: None,
|
|
||||||
modifiers: winit::keyboard::ModifiersState::default(),
|
|
||||||
theme,
|
|
||||||
appearance,
|
|
||||||
application: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current [`Viewport`] of the [`State`].
|
|
||||||
pub fn viewport(&self) -> &Viewport {
|
|
||||||
&self.viewport
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the version of the [`Viewport`] of the [`State`].
|
|
||||||
///
|
|
||||||
/// The version is incremented every time the [`Viewport`] changes.
|
|
||||||
pub fn viewport_version(&self) -> usize {
|
|
||||||
self.viewport_version
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the physical [`Size`] of the [`Viewport`] of the [`State`].
|
|
||||||
pub fn physical_size(&self) -> Size<u32> {
|
|
||||||
self.viewport.physical_size()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the logical [`Size`] of the [`Viewport`] of the [`State`].
|
|
||||||
pub fn logical_size(&self) -> Size<f32> {
|
|
||||||
self.viewport.logical_size()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current scale factor of the [`Viewport`] of the [`State`].
|
|
||||||
pub fn scale_factor(&self) -> f64 {
|
|
||||||
self.viewport.scale_factor()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current cursor position of the [`State`].
|
|
||||||
pub fn cursor(&self) -> mouse::Cursor {
|
|
||||||
self.cursor_position
|
|
||||||
.map(|cursor_position| {
|
|
||||||
conversion::cursor_position(
|
|
||||||
cursor_position,
|
|
||||||
self.viewport.scale_factor(),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.map(mouse::Cursor::Available)
|
|
||||||
.unwrap_or(mouse::Cursor::Unavailable)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current keyboard modifiers of the [`State`].
|
|
||||||
pub fn modifiers(&self) -> winit::keyboard::ModifiersState {
|
|
||||||
self.modifiers
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current theme of the [`State`].
|
|
||||||
pub fn theme(&self) -> &A::Theme {
|
|
||||||
&self.theme
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current background [`Color`] of the [`State`].
|
|
||||||
pub fn background_color(&self) -> Color {
|
|
||||||
self.appearance.background_color
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current text [`Color`] of the [`State`].
|
|
||||||
pub fn text_color(&self) -> Color {
|
|
||||||
self.appearance.text_color
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Processes the provided window event and updates the [`State`]
|
|
||||||
/// accordingly.
|
|
||||||
pub fn update(
|
|
||||||
&mut self,
|
|
||||||
window: &Window,
|
|
||||||
event: &WindowEvent,
|
|
||||||
_debug: &mut Debug,
|
|
||||||
) {
|
|
||||||
match event {
|
|
||||||
WindowEvent::Resized(new_size) => {
|
|
||||||
let size = Size::new(new_size.width, new_size.height);
|
|
||||||
|
|
||||||
self.viewport = Viewport::with_physical_size(
|
|
||||||
size,
|
|
||||||
window.scale_factor() * self.scale_factor,
|
|
||||||
);
|
|
||||||
|
|
||||||
self.viewport_version = self.viewport_version.wrapping_add(1);
|
|
||||||
}
|
|
||||||
WindowEvent::ScaleFactorChanged {
|
|
||||||
scale_factor: new_scale_factor,
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
let size = self.viewport.physical_size();
|
|
||||||
|
|
||||||
self.viewport = Viewport::with_physical_size(
|
|
||||||
size,
|
|
||||||
new_scale_factor * self.scale_factor,
|
|
||||||
);
|
|
||||||
|
|
||||||
self.viewport_version = self.viewport_version.wrapping_add(1);
|
|
||||||
}
|
|
||||||
WindowEvent::CursorMoved { position, .. }
|
|
||||||
| WindowEvent::Touch(Touch {
|
|
||||||
location: position, ..
|
|
||||||
}) => {
|
|
||||||
self.cursor_position = Some(*position);
|
|
||||||
}
|
|
||||||
WindowEvent::CursorLeft { .. } => {
|
|
||||||
self.cursor_position = None;
|
|
||||||
}
|
|
||||||
WindowEvent::ModifiersChanged(new_modifiers) => {
|
|
||||||
self.modifiers = new_modifiers.state();
|
|
||||||
}
|
|
||||||
#[cfg(feature = "debug")]
|
|
||||||
WindowEvent::KeyboardInput {
|
|
||||||
event:
|
|
||||||
winit::event::KeyEvent {
|
|
||||||
logical_key:
|
|
||||||
winit::keyboard::Key::Named(
|
|
||||||
winit::keyboard::NamedKey::F12,
|
|
||||||
),
|
|
||||||
state: winit::event::ElementState::Pressed,
|
|
||||||
..
|
|
||||||
},
|
|
||||||
..
|
|
||||||
} => _debug.toggle(),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Synchronizes the [`State`] with its [`Application`] and its respective
|
|
||||||
/// window.
|
|
||||||
///
|
|
||||||
/// Normally an [`Application`] should be synchronized with its [`State`]
|
|
||||||
/// and window after calling [`crate::application::update`].
|
|
||||||
pub fn synchronize(&mut self, application: &A, window: &Window) {
|
|
||||||
// Update window title
|
|
||||||
let new_title = application.title();
|
|
||||||
|
|
||||||
if self.title != new_title {
|
|
||||||
window.set_title(&new_title);
|
|
||||||
|
|
||||||
self.title = new_title;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update scale factor and size
|
|
||||||
let new_scale_factor = application.scale_factor();
|
|
||||||
let new_size = window.inner_size();
|
|
||||||
let current_size = self.viewport.physical_size();
|
|
||||||
|
|
||||||
if self.scale_factor != new_scale_factor
|
|
||||||
|| (current_size.width, current_size.height)
|
|
||||||
!= (new_size.width, new_size.height)
|
|
||||||
{
|
|
||||||
self.viewport = Viewport::with_physical_size(
|
|
||||||
Size::new(new_size.width, new_size.height),
|
|
||||||
window.scale_factor() * new_scale_factor,
|
|
||||||
);
|
|
||||||
self.viewport_version = self.viewport_version.wrapping_add(1);
|
|
||||||
|
|
||||||
self.scale_factor = new_scale_factor;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update theme and appearance
|
|
||||||
self.theme = application.theme();
|
|
||||||
self.appearance = application.style(&self.theme);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
//! `iced_winit` offers some convenient abstractions on top of [`iced_runtime`]
|
//! `iced_winit` offers some convenient abstractions on top of [`iced_runtime`]
|
||||||
//! to quickstart development when using [`winit`].
|
//! to quickstart development when using [`winit`].
|
||||||
//!
|
//!
|
||||||
//! It exposes a renderer-agnostic [`Application`] trait that can be implemented
|
//! It exposes a renderer-agnostic [`Program`] trait that can be implemented
|
||||||
//! and then run with a simple call. The use of this trait is optional.
|
//! and then run with a simple call. The use of this trait is optional.
|
||||||
//!
|
//!
|
||||||
//! Additionally, a [`conversion`] module is available for users that decide to
|
//! Additionally, a [`conversion`] module is available for users that decide to
|
||||||
|
|
@ -24,24 +24,23 @@ pub use iced_runtime::core;
|
||||||
pub use iced_runtime::futures;
|
pub use iced_runtime::futures;
|
||||||
pub use winit;
|
pub use winit;
|
||||||
|
|
||||||
#[cfg(feature = "multi-window")]
|
|
||||||
pub mod multi_window;
|
|
||||||
|
|
||||||
#[cfg(feature = "application")]
|
|
||||||
pub mod application;
|
|
||||||
pub mod clipboard;
|
pub mod clipboard;
|
||||||
pub mod conversion;
|
pub mod conversion;
|
||||||
pub mod settings;
|
pub mod settings;
|
||||||
|
|
||||||
|
#[cfg(feature = "program")]
|
||||||
|
pub mod program;
|
||||||
|
|
||||||
#[cfg(feature = "system")]
|
#[cfg(feature = "system")]
|
||||||
pub mod system;
|
pub mod system;
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod proxy;
|
mod proxy;
|
||||||
|
|
||||||
#[cfg(feature = "application")]
|
|
||||||
pub use application::Application;
|
|
||||||
pub use clipboard::Clipboard;
|
pub use clipboard::Clipboard;
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
pub use proxy::Proxy;
|
pub use proxy::Proxy;
|
||||||
pub use settings::Settings;
|
pub use settings::Settings;
|
||||||
|
|
||||||
|
#[cfg(feature = "program")]
|
||||||
|
pub use program::Program;
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use crate::core::mouse;
|
||||||
use crate::core::renderer;
|
use crate::core::renderer;
|
||||||
use crate::core::widget::operation;
|
use crate::core::widget::operation;
|
||||||
use crate::core::window;
|
use crate::core::window;
|
||||||
use crate::core::{Point, Size};
|
use crate::core::{Color, Element, Point, Size, Theme};
|
||||||
use crate::futures::futures::channel::mpsc;
|
use crate::futures::futures::channel::mpsc;
|
||||||
use crate::futures::futures::channel::oneshot;
|
use crate::futures::futures::channel::oneshot;
|
||||||
use crate::futures::futures::executor;
|
use crate::futures::futures::executor;
|
||||||
|
|
@ -20,14 +20,12 @@ use crate::futures::subscription::{self, Subscription};
|
||||||
use crate::futures::{Executor, Runtime};
|
use crate::futures::{Executor, Runtime};
|
||||||
use crate::graphics;
|
use crate::graphics;
|
||||||
use crate::graphics::{compositor, Compositor};
|
use crate::graphics::{compositor, Compositor};
|
||||||
use crate::multi_window::window_manager::WindowManager;
|
|
||||||
use crate::runtime::multi_window::Program;
|
|
||||||
use crate::runtime::user_interface::{self, UserInterface};
|
use crate::runtime::user_interface::{self, UserInterface};
|
||||||
use crate::runtime::Debug;
|
use crate::runtime::Debug;
|
||||||
use crate::runtime::{Action, Task};
|
use crate::runtime::{self, Action, Task};
|
||||||
use crate::{Clipboard, Error, Proxy, Settings};
|
use crate::{Clipboard, Error, Proxy, Settings};
|
||||||
|
|
||||||
pub use crate::application::{default, Appearance, DefaultStyle};
|
use window_manager::WindowManager;
|
||||||
|
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use std::mem::ManuallyDrop;
|
use std::mem::ManuallyDrop;
|
||||||
|
|
@ -40,19 +38,37 @@ use std::time::Instant;
|
||||||
/// your GUI application by simply calling [`run`]. It will run in
|
/// your GUI application by simply calling [`run`]. It will run in
|
||||||
/// its own window.
|
/// its own window.
|
||||||
///
|
///
|
||||||
/// An [`Application`] can execute asynchronous actions by returning a
|
/// A [`Program`] can execute asynchronous actions by returning a
|
||||||
/// [`Task`] in some of its methods.
|
/// [`Task`] in some of its methods.
|
||||||
///
|
///
|
||||||
/// When using an [`Application`] with the `debug` feature enabled, a debug view
|
/// When using a [`Program`] with the `debug` feature enabled, a debug view
|
||||||
/// can be toggled by pressing `F12`.
|
/// can be toggled by pressing `F12`.
|
||||||
pub trait Application: Program
|
pub trait Program
|
||||||
where
|
where
|
||||||
|
Self: Sized,
|
||||||
Self::Theme: DefaultStyle,
|
Self::Theme: DefaultStyle,
|
||||||
{
|
{
|
||||||
/// The data needed to initialize your [`Application`].
|
/// The type of __messages__ your [`Program`] will produce.
|
||||||
|
type Message: std::fmt::Debug + Send;
|
||||||
|
|
||||||
|
/// The theme used to draw the [`Program`].
|
||||||
|
type Theme;
|
||||||
|
|
||||||
|
/// The [`Executor`] that will run commands and subscriptions.
|
||||||
|
///
|
||||||
|
/// The [default executor] can be a good starting point!
|
||||||
|
///
|
||||||
|
/// [`Executor`]: Self::Executor
|
||||||
|
/// [default executor]: crate::futures::backend::default::Executor
|
||||||
|
type Executor: Executor;
|
||||||
|
|
||||||
|
/// The graphics backend to use to draw the [`Program`].
|
||||||
|
type Renderer: core::Renderer + core::text::Renderer;
|
||||||
|
|
||||||
|
/// The data needed to initialize your [`Program`].
|
||||||
type Flags;
|
type Flags;
|
||||||
|
|
||||||
/// Initializes the [`Application`] with the flags provided to
|
/// Initializes the [`Program`] with the flags provided to
|
||||||
/// [`run`] as part of the [`Settings`].
|
/// [`run`] as part of the [`Settings`].
|
||||||
///
|
///
|
||||||
/// Here is where you should return the initial state of your app.
|
/// Here is where you should return the initial state of your app.
|
||||||
|
|
@ -62,13 +78,31 @@ where
|
||||||
/// load state from a file, perform an initial HTTP request, etc.
|
/// load state from a file, perform an initial HTTP request, etc.
|
||||||
fn new(flags: Self::Flags) -> (Self, Task<Self::Message>);
|
fn new(flags: Self::Flags) -> (Self, Task<Self::Message>);
|
||||||
|
|
||||||
/// Returns the current title of the [`Application`].
|
/// Returns the current title of the [`Program`].
|
||||||
///
|
///
|
||||||
/// This title can be dynamic! The runtime will automatically update the
|
/// This title can be dynamic! The runtime will automatically update the
|
||||||
/// title of your application when necessary.
|
/// title of your application when necessary.
|
||||||
fn title(&self, window: window::Id) -> String;
|
fn title(&self, window: window::Id) -> String;
|
||||||
|
|
||||||
/// Returns the current `Theme` of the [`Application`].
|
/// Handles a __message__ and updates the state of the [`Program`].
|
||||||
|
///
|
||||||
|
/// This is where you define your __update logic__. All the __messages__,
|
||||||
|
/// produced by either user interactions or commands, will be handled by
|
||||||
|
/// this method.
|
||||||
|
///
|
||||||
|
/// Any [`Task`] returned will be executed immediately in the background by the
|
||||||
|
/// runtime.
|
||||||
|
fn update(&mut self, message: Self::Message) -> Task<Self::Message>;
|
||||||
|
|
||||||
|
/// Returns the widgets to display in the [`Program`] for the `window`.
|
||||||
|
///
|
||||||
|
/// These widgets can produce __messages__ based on user interaction.
|
||||||
|
fn view(
|
||||||
|
&self,
|
||||||
|
window: window::Id,
|
||||||
|
) -> Element<'_, Self::Message, Self::Theme, Self::Renderer>;
|
||||||
|
|
||||||
|
/// Returns the current `Theme` of the [`Program`].
|
||||||
fn theme(&self, window: window::Id) -> Self::Theme;
|
fn theme(&self, window: window::Id) -> Self::Theme;
|
||||||
|
|
||||||
/// Returns the `Style` variation of the `Theme`.
|
/// Returns the `Style` variation of the `Theme`.
|
||||||
|
|
@ -89,7 +123,7 @@ where
|
||||||
Subscription::none()
|
Subscription::none()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the scale factor of the window of the [`Application`].
|
/// Returns the scale factor of the window of the [`Program`].
|
||||||
///
|
///
|
||||||
/// It can be used to dynamically control the size of the UI at runtime
|
/// It can be used to dynamically control the size of the UI at runtime
|
||||||
/// (i.e. zooming).
|
/// (i.e. zooming).
|
||||||
|
|
@ -104,17 +138,49 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs an [`Application`] with an executor, compositor, and the provided
|
/// The appearance of a program.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub struct Appearance {
|
||||||
|
/// The background [`Color`] of the application.
|
||||||
|
pub background_color: Color,
|
||||||
|
|
||||||
|
/// The default text [`Color`] of the application.
|
||||||
|
pub text_color: Color,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The default style of a [`Program`].
|
||||||
|
pub trait DefaultStyle {
|
||||||
|
/// Returns the default style of a [`Program`].
|
||||||
|
fn default_style(&self) -> Appearance;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DefaultStyle for Theme {
|
||||||
|
fn default_style(&self) -> Appearance {
|
||||||
|
default(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The default [`Appearance`] of a [`Program`] with the built-in [`Theme`].
|
||||||
|
pub fn default(theme: &Theme) -> Appearance {
|
||||||
|
let palette = theme.extended_palette();
|
||||||
|
|
||||||
|
Appearance {
|
||||||
|
background_color: palette.background.base.color,
|
||||||
|
text_color: palette.background.base.text,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Runs a [`Program`] with an executor, compositor, and the provided
|
||||||
/// settings.
|
/// settings.
|
||||||
pub fn run<A, E, C>(
|
pub fn run<P, C>(
|
||||||
settings: Settings<A::Flags>,
|
settings: Settings,
|
||||||
graphics_settings: graphics::Settings,
|
graphics_settings: graphics::Settings,
|
||||||
|
window_settings: Option<window::Settings>,
|
||||||
|
flags: P::Flags,
|
||||||
) -> Result<(), Error>
|
) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
A: Application + 'static,
|
P: Program + 'static,
|
||||||
E: Executor + 'static,
|
C: Compositor<Renderer = P::Renderer> + 'static,
|
||||||
C: Compositor<Renderer = A::Renderer> + 'static,
|
P::Theme: DefaultStyle,
|
||||||
A::Theme: DefaultStyle,
|
|
||||||
{
|
{
|
||||||
use winit::event_loop::EventLoop;
|
use winit::event_loop::EventLoop;
|
||||||
|
|
||||||
|
|
@ -128,30 +194,24 @@ where
|
||||||
let (proxy, worker) = Proxy::new(event_loop.create_proxy());
|
let (proxy, worker) = Proxy::new(event_loop.create_proxy());
|
||||||
|
|
||||||
let mut runtime = {
|
let mut runtime = {
|
||||||
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
|
let executor =
|
||||||
|
P::Executor::new().map_err(Error::ExecutorCreationFailed)?;
|
||||||
executor.spawn(worker);
|
executor.spawn(worker);
|
||||||
|
|
||||||
Runtime::new(executor, proxy.clone())
|
Runtime::new(executor, proxy.clone())
|
||||||
};
|
};
|
||||||
|
|
||||||
let (application, task) = {
|
let (application, task) = runtime.enter(|| P::new(flags));
|
||||||
let flags = settings.flags;
|
|
||||||
|
|
||||||
runtime.enter(|| A::new(flags))
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(stream) = task.into_stream() {
|
if let Some(stream) = task.into_stream() {
|
||||||
runtime.run(stream);
|
runtime.run(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = settings.id;
|
|
||||||
let title = application.title(window::Id::MAIN);
|
|
||||||
|
|
||||||
let (boot_sender, boot_receiver) = oneshot::channel();
|
let (boot_sender, boot_receiver) = oneshot::channel();
|
||||||
let (event_sender, event_receiver) = mpsc::unbounded();
|
let (event_sender, event_receiver) = mpsc::unbounded();
|
||||||
let (control_sender, control_receiver) = mpsc::unbounded();
|
let (control_sender, control_receiver) = mpsc::unbounded();
|
||||||
|
|
||||||
let instance = Box::pin(run_instance::<A, E, C>(
|
let instance = Box::pin(run_instance::<P, C>(
|
||||||
application,
|
application,
|
||||||
runtime,
|
runtime,
|
||||||
proxy,
|
proxy,
|
||||||
|
|
@ -166,6 +226,7 @@ where
|
||||||
struct Runner<Message: 'static, F, C> {
|
struct Runner<Message: 'static, F, C> {
|
||||||
instance: std::pin::Pin<Box<F>>,
|
instance: std::pin::Pin<Box<F>>,
|
||||||
context: task::Context<'static>,
|
context: task::Context<'static>,
|
||||||
|
id: Option<String>,
|
||||||
boot: Option<BootConfig<C>>,
|
boot: Option<BootConfig<C>>,
|
||||||
sender: mpsc::UnboundedSender<Event<Message>>,
|
sender: mpsc::UnboundedSender<Event<Message>>,
|
||||||
receiver: mpsc::UnboundedReceiver<Control>,
|
receiver: mpsc::UnboundedReceiver<Control>,
|
||||||
|
|
@ -174,20 +235,17 @@ where
|
||||||
|
|
||||||
struct BootConfig<C> {
|
struct BootConfig<C> {
|
||||||
sender: oneshot::Sender<Boot<C>>,
|
sender: oneshot::Sender<Boot<C>>,
|
||||||
id: Option<String>,
|
window_settings: Option<window::Settings>,
|
||||||
title: String,
|
|
||||||
window_settings: window::Settings,
|
|
||||||
graphics_settings: graphics::Settings,
|
graphics_settings: graphics::Settings,
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut runner = Runner {
|
let mut runner = Runner {
|
||||||
instance,
|
instance,
|
||||||
context,
|
context,
|
||||||
|
id: settings.id,
|
||||||
boot: Some(BootConfig {
|
boot: Some(BootConfig {
|
||||||
sender: boot_sender,
|
sender: boot_sender,
|
||||||
id,
|
window_settings,
|
||||||
title,
|
|
||||||
window_settings: settings.window,
|
|
||||||
graphics_settings,
|
graphics_settings,
|
||||||
}),
|
}),
|
||||||
sender: event_sender,
|
sender: event_sender,
|
||||||
|
|
@ -204,8 +262,6 @@ where
|
||||||
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
|
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
|
||||||
let Some(BootConfig {
|
let Some(BootConfig {
|
||||||
sender,
|
sender,
|
||||||
id,
|
|
||||||
title,
|
|
||||||
window_settings,
|
window_settings,
|
||||||
graphics_settings,
|
graphics_settings,
|
||||||
}) = self.boot.take()
|
}) = self.boot.take()
|
||||||
|
|
@ -213,20 +269,9 @@ where
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let should_be_visible = window_settings.visible;
|
let window = match event_loop.create_window(
|
||||||
let exit_on_close_request = window_settings.exit_on_close_request;
|
winit::window::WindowAttributes::default().with_visible(false),
|
||||||
|
) {
|
||||||
let window_attributes = conversion::window_attributes(
|
|
||||||
window_settings,
|
|
||||||
&title,
|
|
||||||
event_loop.primary_monitor(),
|
|
||||||
id,
|
|
||||||
)
|
|
||||||
.with_visible(false);
|
|
||||||
|
|
||||||
log::debug!("Window attributes: {window_attributes:#?}");
|
|
||||||
|
|
||||||
let window = match event_loop.create_window(window_attributes) {
|
|
||||||
Ok(window) => Arc::new(window),
|
Ok(window) => Arc::new(window),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
self.error = Some(Error::WindowCreationFailed(error));
|
self.error = Some(Error::WindowCreationFailed(error));
|
||||||
|
|
@ -235,16 +280,17 @@ where
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let clipboard = Clipboard::connect(&window);
|
||||||
|
|
||||||
let finish_boot = async move {
|
let finish_boot = async move {
|
||||||
let compositor =
|
let compositor =
|
||||||
C::new(graphics_settings, window.clone()).await?;
|
C::new(graphics_settings, window.clone()).await?;
|
||||||
|
|
||||||
sender
|
sender
|
||||||
.send(Boot {
|
.send(Boot {
|
||||||
window,
|
|
||||||
compositor,
|
compositor,
|
||||||
should_be_visible,
|
clipboard,
|
||||||
exit_on_close_request,
|
window_settings,
|
||||||
})
|
})
|
||||||
.ok()
|
.ok()
|
||||||
.expect("Send boot event");
|
.expect("Send boot event");
|
||||||
|
|
@ -386,7 +432,12 @@ where
|
||||||
let window = event_loop
|
let window = event_loop
|
||||||
.create_window(
|
.create_window(
|
||||||
conversion::window_attributes(
|
conversion::window_attributes(
|
||||||
settings, &title, monitor, None,
|
settings,
|
||||||
|
&title,
|
||||||
|
monitor
|
||||||
|
.or(event_loop
|
||||||
|
.primary_monitor()),
|
||||||
|
self.id.clone(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.expect("Create window");
|
.expect("Create window");
|
||||||
|
|
@ -423,10 +474,9 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Boot<C> {
|
struct Boot<C> {
|
||||||
window: Arc<winit::window::Window>,
|
|
||||||
compositor: C,
|
compositor: C,
|
||||||
should_be_visible: bool,
|
clipboard: Clipboard,
|
||||||
exit_on_close_request: bool,
|
window_settings: Option<window::Settings>,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Event<Message: 'static> {
|
enum Event<Message: 'static> {
|
||||||
|
|
@ -449,62 +499,37 @@ enum Control {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run_instance<A, E, C>(
|
async fn run_instance<P, C>(
|
||||||
mut application: A,
|
mut program: P,
|
||||||
mut runtime: Runtime<E, Proxy<A::Message>, Action<A::Message>>,
|
mut runtime: Runtime<P::Executor, Proxy<P::Message>, Action<P::Message>>,
|
||||||
mut proxy: Proxy<A::Message>,
|
mut proxy: Proxy<P::Message>,
|
||||||
mut debug: Debug,
|
mut debug: Debug,
|
||||||
mut boot: oneshot::Receiver<Boot<C>>,
|
mut boot: oneshot::Receiver<Boot<C>>,
|
||||||
mut event_receiver: mpsc::UnboundedReceiver<Event<Action<A::Message>>>,
|
mut event_receiver: mpsc::UnboundedReceiver<Event<Action<P::Message>>>,
|
||||||
mut control_sender: mpsc::UnboundedSender<Control>,
|
mut control_sender: mpsc::UnboundedSender<Control>,
|
||||||
) where
|
) where
|
||||||
A: Application + 'static,
|
P: Program + 'static,
|
||||||
E: Executor + 'static,
|
C: Compositor<Renderer = P::Renderer> + 'static,
|
||||||
C: Compositor<Renderer = A::Renderer> + 'static,
|
P::Theme: DefaultStyle,
|
||||||
A::Theme: DefaultStyle,
|
|
||||||
{
|
{
|
||||||
use winit::event;
|
use winit::event;
|
||||||
use winit::event_loop::ControlFlow;
|
use winit::event_loop::ControlFlow;
|
||||||
|
|
||||||
let Boot {
|
let Boot {
|
||||||
window: main_window,
|
|
||||||
mut compositor,
|
mut compositor,
|
||||||
should_be_visible,
|
mut clipboard,
|
||||||
exit_on_close_request,
|
window_settings,
|
||||||
} = boot.try_recv().ok().flatten().expect("Receive boot");
|
} = boot.try_recv().ok().flatten().expect("Receive boot");
|
||||||
|
|
||||||
let mut window_manager = WindowManager::new();
|
let mut window_manager = WindowManager::new();
|
||||||
|
|
||||||
let _ = window_manager.insert(
|
let mut events = Vec::new();
|
||||||
window::Id::MAIN,
|
let mut messages = Vec::new();
|
||||||
main_window,
|
let mut actions = 0;
|
||||||
&application,
|
|
||||||
&mut compositor,
|
|
||||||
exit_on_close_request,
|
|
||||||
);
|
|
||||||
|
|
||||||
let main_window = window_manager
|
|
||||||
.get_mut(window::Id::MAIN)
|
|
||||||
.expect("Get main window");
|
|
||||||
|
|
||||||
if should_be_visible {
|
|
||||||
main_window.raw.set_visible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut clipboard = Clipboard::connect(&main_window.raw);
|
|
||||||
let mut events = {
|
|
||||||
vec![(
|
|
||||||
window::Id::MAIN,
|
|
||||||
core::Event::Window(window::Event::Opened {
|
|
||||||
position: main_window.position(),
|
|
||||||
size: main_window.size(),
|
|
||||||
}),
|
|
||||||
)]
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut ui_caches = FxHashMap::default();
|
let mut ui_caches = FxHashMap::default();
|
||||||
let mut user_interfaces = ManuallyDrop::new(build_user_interfaces(
|
let mut user_interfaces = ManuallyDrop::new(build_user_interfaces(
|
||||||
&application,
|
&program,
|
||||||
&mut debug,
|
&mut debug,
|
||||||
&mut window_manager,
|
&mut window_manager,
|
||||||
FxHashMap::from_iter([(
|
FxHashMap::from_iter([(
|
||||||
|
|
@ -513,15 +538,19 @@ async fn run_instance<A, E, C>(
|
||||||
)]),
|
)]),
|
||||||
));
|
));
|
||||||
|
|
||||||
runtime.track(
|
runtime.track(program.subscription().map(Action::Output).into_recipes());
|
||||||
application
|
|
||||||
.subscription()
|
|
||||||
.map(Action::Output)
|
|
||||||
.into_recipes(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut messages = Vec::new();
|
let is_daemon = window_settings.is_none();
|
||||||
let mut user_events = 0;
|
|
||||||
|
if let Some(window_settings) = window_settings {
|
||||||
|
let (sender, _receiver) = oneshot::channel();
|
||||||
|
|
||||||
|
proxy.send_action(Action::Window(runtime::window::Action::Open(
|
||||||
|
window::Id::unique(),
|
||||||
|
window_settings,
|
||||||
|
sender,
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
debug.startup_finished();
|
debug.startup_finished();
|
||||||
|
|
||||||
|
|
@ -535,7 +564,7 @@ async fn run_instance<A, E, C>(
|
||||||
let window = window_manager.insert(
|
let window = window_manager.insert(
|
||||||
id,
|
id,
|
||||||
Arc::new(window),
|
Arc::new(window),
|
||||||
&application,
|
&program,
|
||||||
&mut compositor,
|
&mut compositor,
|
||||||
exit_on_close_request,
|
exit_on_close_request,
|
||||||
);
|
);
|
||||||
|
|
@ -545,7 +574,7 @@ async fn run_instance<A, E, C>(
|
||||||
let _ = user_interfaces.insert(
|
let _ = user_interfaces.insert(
|
||||||
id,
|
id,
|
||||||
build_user_interface(
|
build_user_interface(
|
||||||
&application,
|
&program,
|
||||||
user_interface::Cache::default(),
|
user_interface::Cache::default(),
|
||||||
&mut window.renderer,
|
&mut window.renderer,
|
||||||
logical_size,
|
logical_size,
|
||||||
|
|
@ -591,7 +620,7 @@ async fn run_instance<A, E, C>(
|
||||||
event::Event::UserEvent(action) => {
|
event::Event::UserEvent(action) => {
|
||||||
run_action(
|
run_action(
|
||||||
action,
|
action,
|
||||||
&application,
|
&program,
|
||||||
&mut compositor,
|
&mut compositor,
|
||||||
&mut messages,
|
&mut messages,
|
||||||
&mut clipboard,
|
&mut clipboard,
|
||||||
|
|
@ -601,7 +630,7 @@ async fn run_instance<A, E, C>(
|
||||||
&mut window_manager,
|
&mut window_manager,
|
||||||
&mut ui_caches,
|
&mut ui_caches,
|
||||||
);
|
);
|
||||||
user_events += 1;
|
actions += 1;
|
||||||
}
|
}
|
||||||
event::Event::WindowEvent {
|
event::Event::WindowEvent {
|
||||||
window_id: id,
|
window_id: id,
|
||||||
|
|
@ -782,6 +811,16 @@ async fn run_instance<A, E, C>(
|
||||||
event: window_event,
|
event: window_event,
|
||||||
window_id,
|
window_id,
|
||||||
} => {
|
} => {
|
||||||
|
if !is_daemon
|
||||||
|
&& matches!(
|
||||||
|
window_event,
|
||||||
|
winit::event::WindowEvent::Destroyed
|
||||||
|
)
|
||||||
|
&& window_manager.is_empty()
|
||||||
|
{
|
||||||
|
break 'main;
|
||||||
|
}
|
||||||
|
|
||||||
let Some((id, window)) =
|
let Some((id, window)) =
|
||||||
window_manager.get_mut_alias(window_id)
|
window_manager.get_mut_alias(window_id)
|
||||||
else {
|
else {
|
||||||
|
|
@ -801,10 +840,6 @@ async fn run_instance<A, E, C>(
|
||||||
id,
|
id,
|
||||||
core::Event::Window(window::Event::Closed),
|
core::Event::Window(window::Event::Closed),
|
||||||
));
|
));
|
||||||
|
|
||||||
if window_manager.is_empty() {
|
|
||||||
break 'main;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
window.state.update(
|
window.state.update(
|
||||||
&window.raw,
|
&window.raw,
|
||||||
|
|
@ -903,7 +938,7 @@ async fn run_instance<A, E, C>(
|
||||||
|
|
||||||
// Update application
|
// Update application
|
||||||
update(
|
update(
|
||||||
&mut application,
|
&mut program,
|
||||||
&mut runtime,
|
&mut runtime,
|
||||||
&mut debug,
|
&mut debug,
|
||||||
&mut messages,
|
&mut messages,
|
||||||
|
|
@ -913,7 +948,7 @@ async fn run_instance<A, E, C>(
|
||||||
// application update since we don't know what changed
|
// application update since we don't know what changed
|
||||||
for (id, window) in window_manager.iter_mut() {
|
for (id, window) in window_manager.iter_mut() {
|
||||||
window.state.synchronize(
|
window.state.synchronize(
|
||||||
&application,
|
&program,
|
||||||
id,
|
id,
|
||||||
&window.raw,
|
&window.raw,
|
||||||
);
|
);
|
||||||
|
|
@ -926,15 +961,15 @@ async fn run_instance<A, E, C>(
|
||||||
// rebuild UIs with the synchronized states
|
// rebuild UIs with the synchronized states
|
||||||
user_interfaces =
|
user_interfaces =
|
||||||
ManuallyDrop::new(build_user_interfaces(
|
ManuallyDrop::new(build_user_interfaces(
|
||||||
&application,
|
&program,
|
||||||
&mut debug,
|
&mut debug,
|
||||||
&mut window_manager,
|
&mut window_manager,
|
||||||
cached_interfaces,
|
cached_interfaces,
|
||||||
));
|
));
|
||||||
|
|
||||||
if user_events > 0 {
|
if actions > 0 {
|
||||||
proxy.free_slots(user_events);
|
proxy.free_slots(actions);
|
||||||
user_events = 0;
|
actions = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -947,17 +982,17 @@ async fn run_instance<A, E, C>(
|
||||||
let _ = ManuallyDrop::into_inner(user_interfaces);
|
let _ = ManuallyDrop::into_inner(user_interfaces);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds a window's [`UserInterface`] for the [`Application`].
|
/// Builds a window's [`UserInterface`] for the [`Program`].
|
||||||
fn build_user_interface<'a, A: Application>(
|
fn build_user_interface<'a, P: Program>(
|
||||||
application: &'a A,
|
application: &'a P,
|
||||||
cache: user_interface::Cache,
|
cache: user_interface::Cache,
|
||||||
renderer: &mut A::Renderer,
|
renderer: &mut P::Renderer,
|
||||||
size: Size,
|
size: Size,
|
||||||
debug: &mut Debug,
|
debug: &mut Debug,
|
||||||
id: window::Id,
|
id: window::Id,
|
||||||
) -> UserInterface<'a, A::Message, A::Theme, A::Renderer>
|
) -> UserInterface<'a, P::Message, P::Theme, P::Renderer>
|
||||||
where
|
where
|
||||||
A::Theme: DefaultStyle,
|
P::Theme: DefaultStyle,
|
||||||
{
|
{
|
||||||
debug.view_started();
|
debug.view_started();
|
||||||
let view = application.view(id);
|
let view = application.view(id);
|
||||||
|
|
@ -970,13 +1005,13 @@ where
|
||||||
user_interface
|
user_interface
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update<A: Application, E: Executor>(
|
fn update<P: Program, E: Executor>(
|
||||||
application: &mut A,
|
application: &mut P,
|
||||||
runtime: &mut Runtime<E, Proxy<A::Message>, Action<A::Message>>,
|
runtime: &mut Runtime<E, Proxy<P::Message>, Action<P::Message>>,
|
||||||
debug: &mut Debug,
|
debug: &mut Debug,
|
||||||
messages: &mut Vec<A::Message>,
|
messages: &mut Vec<P::Message>,
|
||||||
) where
|
) where
|
||||||
A::Theme: DefaultStyle,
|
P::Theme: DefaultStyle,
|
||||||
{
|
{
|
||||||
for message in messages.drain(..) {
|
for message in messages.drain(..) {
|
||||||
debug.log_message(&message);
|
debug.log_message(&message);
|
||||||
|
|
@ -994,24 +1029,24 @@ fn update<A: Application, E: Executor>(
|
||||||
runtime.track(subscription.map(Action::Output).into_recipes());
|
runtime.track(subscription.map(Action::Output).into_recipes());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_action<A, C>(
|
fn run_action<P, C>(
|
||||||
action: Action<A::Message>,
|
action: Action<P::Message>,
|
||||||
application: &A,
|
application: &P,
|
||||||
compositor: &mut C,
|
compositor: &mut C,
|
||||||
messages: &mut Vec<A::Message>,
|
messages: &mut Vec<P::Message>,
|
||||||
clipboard: &mut Clipboard,
|
clipboard: &mut Clipboard,
|
||||||
control_sender: &mut mpsc::UnboundedSender<Control>,
|
control_sender: &mut mpsc::UnboundedSender<Control>,
|
||||||
debug: &mut Debug,
|
debug: &mut Debug,
|
||||||
interfaces: &mut FxHashMap<
|
interfaces: &mut FxHashMap<
|
||||||
window::Id,
|
window::Id,
|
||||||
UserInterface<'_, A::Message, A::Theme, A::Renderer>,
|
UserInterface<'_, P::Message, P::Theme, P::Renderer>,
|
||||||
>,
|
>,
|
||||||
window_manager: &mut WindowManager<A, C>,
|
window_manager: &mut WindowManager<P, C>,
|
||||||
ui_caches: &mut FxHashMap<window::Id, user_interface::Cache>,
|
ui_caches: &mut FxHashMap<window::Id, user_interface::Cache>,
|
||||||
) where
|
) where
|
||||||
A: Application,
|
P: Program,
|
||||||
C: Compositor<Renderer = A::Renderer> + 'static,
|
C: Compositor<Renderer = P::Renderer> + 'static,
|
||||||
A::Theme: DefaultStyle,
|
P::Theme: DefaultStyle,
|
||||||
{
|
{
|
||||||
use crate::runtime::clipboard;
|
use crate::runtime::clipboard;
|
||||||
use crate::runtime::system;
|
use crate::runtime::system;
|
||||||
|
|
@ -1047,12 +1082,6 @@ fn run_action<A, C>(
|
||||||
window::Action::Close(id) => {
|
window::Action::Close(id) => {
|
||||||
let _ = window_manager.remove(id);
|
let _ = window_manager.remove(id);
|
||||||
let _ = ui_caches.remove(&id);
|
let _ = ui_caches.remove(&id);
|
||||||
|
|
||||||
if window_manager.is_empty() {
|
|
||||||
control_sender
|
|
||||||
.start_send(Control::Exit)
|
|
||||||
.expect("Send control action");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
window::Action::Drag(id) => {
|
window::Action::Drag(id) => {
|
||||||
if let Some(window) = window_manager.get_mut(id) {
|
if let Some(window) = window_manager.get_mut(id) {
|
||||||
|
|
@ -1266,19 +1295,24 @@ fn run_action<A, C>(
|
||||||
|
|
||||||
let _ = channel.send(Ok(()));
|
let _ = channel.send(Ok(()));
|
||||||
}
|
}
|
||||||
|
Action::Exit => {
|
||||||
|
control_sender
|
||||||
|
.start_send(Control::Exit)
|
||||||
|
.expect("Send control action");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build the user interface for every window.
|
/// Build the user interface for every window.
|
||||||
pub fn build_user_interfaces<'a, A: Application, C>(
|
pub fn build_user_interfaces<'a, P: Program, C>(
|
||||||
application: &'a A,
|
application: &'a P,
|
||||||
debug: &mut Debug,
|
debug: &mut Debug,
|
||||||
window_manager: &mut WindowManager<A, C>,
|
window_manager: &mut WindowManager<P, C>,
|
||||||
mut cached_user_interfaces: FxHashMap<window::Id, user_interface::Cache>,
|
mut cached_user_interfaces: FxHashMap<window::Id, user_interface::Cache>,
|
||||||
) -> FxHashMap<window::Id, UserInterface<'a, A::Message, A::Theme, A::Renderer>>
|
) -> FxHashMap<window::Id, UserInterface<'a, P::Message, P::Theme, P::Renderer>>
|
||||||
where
|
where
|
||||||
C: Compositor<Renderer = A::Renderer>,
|
C: Compositor<Renderer = P::Renderer>,
|
||||||
A::Theme: DefaultStyle,
|
P::Theme: DefaultStyle,
|
||||||
{
|
{
|
||||||
cached_user_interfaces
|
cached_user_interfaces
|
||||||
.drain()
|
.drain()
|
||||||
|
|
@ -1300,7 +1334,7 @@ where
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the provided event should cause an [`Application`] to
|
/// Returns true if the provided event should cause a [`Program`] to
|
||||||
/// exit.
|
/// exit.
|
||||||
pub fn user_force_quit(
|
pub fn user_force_quit(
|
||||||
event: &winit::event::WindowEvent,
|
event: &winit::event::WindowEvent,
|
||||||
|
|
@ -2,16 +2,16 @@ use crate::conversion;
|
||||||
use crate::core::{mouse, window};
|
use crate::core::{mouse, window};
|
||||||
use crate::core::{Color, Size};
|
use crate::core::{Color, Size};
|
||||||
use crate::graphics::Viewport;
|
use crate::graphics::Viewport;
|
||||||
use crate::multi_window::{self, Application};
|
use crate::program::{self, Program};
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
|
|
||||||
use winit::event::{Touch, WindowEvent};
|
use winit::event::{Touch, WindowEvent};
|
||||||
use winit::window::Window;
|
use winit::window::Window;
|
||||||
|
|
||||||
/// The state of a multi-windowed [`Application`].
|
/// The state of a multi-windowed [`Program`].
|
||||||
pub struct State<A: Application>
|
pub struct State<P: Program>
|
||||||
where
|
where
|
||||||
A::Theme: multi_window::DefaultStyle,
|
P::Theme: program::DefaultStyle,
|
||||||
{
|
{
|
||||||
title: String,
|
title: String,
|
||||||
scale_factor: f64,
|
scale_factor: f64,
|
||||||
|
|
@ -19,13 +19,13 @@ where
|
||||||
viewport_version: u64,
|
viewport_version: u64,
|
||||||
cursor_position: Option<winit::dpi::PhysicalPosition<f64>>,
|
cursor_position: Option<winit::dpi::PhysicalPosition<f64>>,
|
||||||
modifiers: winit::keyboard::ModifiersState,
|
modifiers: winit::keyboard::ModifiersState,
|
||||||
theme: A::Theme,
|
theme: P::Theme,
|
||||||
appearance: multi_window::Appearance,
|
appearance: program::Appearance,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A: Application> Debug for State<A>
|
impl<P: Program> Debug for State<P>
|
||||||
where
|
where
|
||||||
A::Theme: multi_window::DefaultStyle,
|
P::Theme: program::DefaultStyle,
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_struct("multi_window::State")
|
f.debug_struct("multi_window::State")
|
||||||
|
|
@ -39,13 +39,13 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A: Application> State<A>
|
impl<P: Program> State<P>
|
||||||
where
|
where
|
||||||
A::Theme: multi_window::DefaultStyle,
|
P::Theme: program::DefaultStyle,
|
||||||
{
|
{
|
||||||
/// Creates a new [`State`] for the provided [`Application`]'s `window`.
|
/// Creates a new [`State`] for the provided [`Program`]'s `window`.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
application: &A,
|
application: &P,
|
||||||
window_id: window::Id,
|
window_id: window::Id,
|
||||||
window: &Window,
|
window: &Window,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
|
@ -121,7 +121,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the current theme of the [`State`].
|
/// Returns the current theme of the [`State`].
|
||||||
pub fn theme(&self) -> &A::Theme {
|
pub fn theme(&self) -> &P::Theme {
|
||||||
&self.theme
|
&self.theme
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -195,14 +195,14 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Synchronizes the [`State`] with its [`Application`] and its respective
|
/// Synchronizes the [`State`] with its [`Program`] and its respective
|
||||||
/// window.
|
/// window.
|
||||||
///
|
///
|
||||||
/// Normally, an [`Application`] should be synchronized with its [`State`]
|
/// Normally, a [`Program`] should be synchronized with its [`State`]
|
||||||
/// and window after calling [`State::update`].
|
/// and window after calling [`State::update`].
|
||||||
pub fn synchronize(
|
pub fn synchronize(
|
||||||
&mut self,
|
&mut self,
|
||||||
application: &A,
|
application: &P,
|
||||||
window_id: window::Id,
|
window_id: window::Id,
|
||||||
window: &Window,
|
window: &Window,
|
||||||
) {
|
) {
|
||||||
|
|
@ -2,28 +2,28 @@ use crate::core::mouse;
|
||||||
use crate::core::window::Id;
|
use crate::core::window::Id;
|
||||||
use crate::core::{Point, Size};
|
use crate::core::{Point, Size};
|
||||||
use crate::graphics::Compositor;
|
use crate::graphics::Compositor;
|
||||||
use crate::multi_window::{Application, DefaultStyle, State};
|
use crate::program::{DefaultStyle, Program, State};
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use winit::monitor::MonitorHandle;
|
use winit::monitor::MonitorHandle;
|
||||||
|
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct WindowManager<A, C>
|
pub struct WindowManager<P, C>
|
||||||
where
|
where
|
||||||
A: Application,
|
P: Program,
|
||||||
C: Compositor<Renderer = A::Renderer>,
|
C: Compositor<Renderer = P::Renderer>,
|
||||||
A::Theme: DefaultStyle,
|
P::Theme: DefaultStyle,
|
||||||
{
|
{
|
||||||
aliases: BTreeMap<winit::window::WindowId, Id>,
|
aliases: BTreeMap<winit::window::WindowId, Id>,
|
||||||
entries: BTreeMap<Id, Window<A, C>>,
|
entries: BTreeMap<Id, Window<P, C>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, C> WindowManager<A, C>
|
impl<P, C> WindowManager<P, C>
|
||||||
where
|
where
|
||||||
A: Application,
|
P: Program,
|
||||||
C: Compositor<Renderer = A::Renderer>,
|
C: Compositor<Renderer = P::Renderer>,
|
||||||
A::Theme: DefaultStyle,
|
P::Theme: DefaultStyle,
|
||||||
{
|
{
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
@ -36,10 +36,10 @@ where
|
||||||
&mut self,
|
&mut self,
|
||||||
id: Id,
|
id: Id,
|
||||||
window: Arc<winit::window::Window>,
|
window: Arc<winit::window::Window>,
|
||||||
application: &A,
|
application: &P,
|
||||||
compositor: &mut C,
|
compositor: &mut C,
|
||||||
exit_on_close_request: bool,
|
exit_on_close_request: bool,
|
||||||
) -> &mut Window<A, C> {
|
) -> &mut Window<P, C> {
|
||||||
let state = State::new(application, id, &window);
|
let state = State::new(application, id, &window);
|
||||||
let viewport_version = state.viewport_version();
|
let viewport_version = state.viewport_version();
|
||||||
let physical_size = state.physical_size();
|
let physical_size = state.physical_size();
|
||||||
|
|
@ -76,18 +76,18 @@ where
|
||||||
|
|
||||||
pub fn iter_mut(
|
pub fn iter_mut(
|
||||||
&mut self,
|
&mut self,
|
||||||
) -> impl Iterator<Item = (Id, &mut Window<A, C>)> {
|
) -> impl Iterator<Item = (Id, &mut Window<P, C>)> {
|
||||||
self.entries.iter_mut().map(|(k, v)| (*k, v))
|
self.entries.iter_mut().map(|(k, v)| (*k, v))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut(&mut self, id: Id) -> Option<&mut Window<A, C>> {
|
pub fn get_mut(&mut self, id: Id) -> Option<&mut Window<P, C>> {
|
||||||
self.entries.get_mut(&id)
|
self.entries.get_mut(&id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut_alias(
|
pub fn get_mut_alias(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: winit::window::WindowId,
|
id: winit::window::WindowId,
|
||||||
) -> Option<(Id, &mut Window<A, C>)> {
|
) -> Option<(Id, &mut Window<P, C>)> {
|
||||||
let id = self.aliases.get(&id).copied()?;
|
let id = self.aliases.get(&id).copied()?;
|
||||||
|
|
||||||
Some((id, self.get_mut(id)?))
|
Some((id, self.get_mut(id)?))
|
||||||
|
|
@ -97,7 +97,7 @@ where
|
||||||
self.entries.values().last()?.raw.current_monitor()
|
self.entries.values().last()?.raw.current_monitor()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove(&mut self, id: Id) -> Option<Window<A, C>> {
|
pub fn remove(&mut self, id: Id) -> Option<Window<P, C>> {
|
||||||
let window = self.entries.remove(&id)?;
|
let window = self.entries.remove(&id)?;
|
||||||
let _ = self.aliases.remove(&window.raw.id());
|
let _ = self.aliases.remove(&window.raw.id());
|
||||||
|
|
||||||
|
|
@ -105,11 +105,11 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, C> Default for WindowManager<A, C>
|
impl<P, C> Default for WindowManager<P, C>
|
||||||
where
|
where
|
||||||
A: Application,
|
P: Program,
|
||||||
C: Compositor<Renderer = A::Renderer>,
|
C: Compositor<Renderer = P::Renderer>,
|
||||||
A::Theme: DefaultStyle,
|
P::Theme: DefaultStyle,
|
||||||
{
|
{
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
|
|
@ -117,26 +117,26 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Window<A, C>
|
pub struct Window<P, C>
|
||||||
where
|
where
|
||||||
A: Application,
|
P: Program,
|
||||||
C: Compositor<Renderer = A::Renderer>,
|
C: Compositor<Renderer = P::Renderer>,
|
||||||
A::Theme: DefaultStyle,
|
P::Theme: DefaultStyle,
|
||||||
{
|
{
|
||||||
pub raw: Arc<winit::window::Window>,
|
pub raw: Arc<winit::window::Window>,
|
||||||
pub state: State<A>,
|
pub state: State<P>,
|
||||||
pub viewport_version: u64,
|
pub viewport_version: u64,
|
||||||
pub exit_on_close_request: bool,
|
pub exit_on_close_request: bool,
|
||||||
pub mouse_interaction: mouse::Interaction,
|
pub mouse_interaction: mouse::Interaction,
|
||||||
pub surface: C::Surface,
|
pub surface: C::Surface,
|
||||||
pub renderer: A::Renderer,
|
pub renderer: P::Renderer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, C> Window<A, C>
|
impl<P, C> Window<P, C>
|
||||||
where
|
where
|
||||||
A: Application,
|
P: Program,
|
||||||
C: Compositor<Renderer = A::Renderer>,
|
C: Compositor<Renderer = P::Renderer>,
|
||||||
A::Theme: DefaultStyle,
|
P::Theme: DefaultStyle,
|
||||||
{
|
{
|
||||||
pub fn position(&self) -> Option<Point> {
|
pub fn position(&self) -> Option<Point> {
|
||||||
self.raw
|
self.raw
|
||||||
|
|
@ -78,11 +78,22 @@ impl<T: 'static> Proxy<T> {
|
||||||
/// Note: This skips the backpressure mechanism with an unbounded
|
/// Note: This skips the backpressure mechanism with an unbounded
|
||||||
/// channel. Use sparingly!
|
/// channel. Use sparingly!
|
||||||
pub fn send(&mut self, value: T)
|
pub fn send(&mut self, value: T)
|
||||||
|
where
|
||||||
|
T: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
self.send_action(Action::Output(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends an action to the event loop.
|
||||||
|
///
|
||||||
|
/// Note: This skips the backpressure mechanism with an unbounded
|
||||||
|
/// channel. Use sparingly!
|
||||||
|
pub fn send_action(&mut self, action: Action<T>)
|
||||||
where
|
where
|
||||||
T: std::fmt::Debug,
|
T: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
self.raw
|
self.raw
|
||||||
.send_event(Action::Output(value))
|
.send_event(action)
|
||||||
.expect("Send message to event loop");
|
.expect("Send message to event loop");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,15 @@
|
||||||
//! Configure your application.
|
//! Configure your application.
|
||||||
use crate::core::window;
|
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
/// The settings of an application.
|
/// The settings of an application.
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct Settings<Flags> {
|
pub struct Settings {
|
||||||
/// The identifier of the application.
|
/// The identifier of the application.
|
||||||
///
|
///
|
||||||
/// If provided, this identifier may be used to identify the application or
|
/// If provided, this identifier may be used to identify the application or
|
||||||
/// communicate with it through the windowing system.
|
/// communicate with it through the windowing system.
|
||||||
pub id: Option<String>,
|
pub id: Option<String>,
|
||||||
|
|
||||||
/// The [`window::Settings`].
|
|
||||||
pub window: window::Settings,
|
|
||||||
|
|
||||||
/// The data needed to initialize an [`Application`].
|
|
||||||
///
|
|
||||||
/// [`Application`]: crate::Application
|
|
||||||
pub flags: Flags,
|
|
||||||
|
|
||||||
/// The fonts to load on boot.
|
/// The fonts to load on boot.
|
||||||
pub fonts: Vec<Cow<'static, [u8]>>,
|
pub fonts: Vec<Cow<'static, [u8]>>,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue