summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-19 20:41:52 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-19 20:41:52 +0100
commit1781068e1c3a65551db1e832fdbaddba99124051 (patch)
tree60e0b3854cc0541712572fbb0e56f14435951ea9 /src
parent41dec5bd203ff5b1574a33a17d5f7358ae1beea2 (diff)
parent7ae7fcb89855002519bab752fd3686106ce448db (diff)
downloadiced-1781068e1c3a65551db1e832fdbaddba99124051.tar.gz
iced-1781068e1c3a65551db1e832fdbaddba99124051.tar.bz2
iced-1781068e1c3a65551db1e832fdbaddba99124051.zip
Merge branch 'master' into remove-vertex-indexing
Diffstat (limited to '')
-rw-r--r--core/src/window/settings.rs (renamed from src/window/settings.rs)73
-rw-r--r--src/lib.rs16
-rw-r--r--src/multi_window.rs4
-rw-r--r--src/multi_window/application.rs245
-rw-r--r--src/settings.rs21
-rw-r--r--src/time.rs1
-rw-r--r--src/window.rs4
-rw-r--r--src/window/position.rs32
8 files changed, 319 insertions, 77 deletions
diff --git a/src/window/settings.rs b/core/src/window/settings.rs
index 0ee573e5..fbbf86ab 100644
--- a/src/window/settings.rs
+++ b/core/src/window/settings.rs
@@ -1,21 +1,47 @@
-use crate::window::{Icon, Level, Position};
+//! Configure your windows.
+#[cfg(target_os = "windows")]
+#[path = "settings/windows.rs"]
+mod platform;
+
+#[cfg(target_os = "macos")]
+#[path = "settings/macos.rs"]
+mod platform;
+
+#[cfg(target_os = "linux")]
+#[path = "settings/linux.rs"]
+mod platform;
+
+#[cfg(target_arch = "wasm32")]
+#[path = "settings/wasm.rs"]
+mod platform;
-pub use iced_winit::settings::PlatformSpecific;
+#[cfg(not(any(
+ target_os = "windows",
+ target_os = "macos",
+ target_os = "linux",
+ target_arch = "wasm32"
+)))]
+#[path = "settings/other.rs"]
+mod platform;
+use crate::window::{Icon, Level, Position};
+use crate::Size;
+
+pub use platform::PlatformSpecific;
/// The window settings of an application.
#[derive(Debug, Clone)]
pub struct Settings {
- /// The initial size of the window.
- pub size: (u32, u32),
+ /// The initial logical dimensions of the window.
+ pub size: Size,
/// The initial position of the window.
pub position: Position,
/// The minimum size of the window.
- pub min_size: Option<(u32, u32)>,
+ pub min_size: Option<Size>,
/// The maximum size of the window.
- pub max_size: Option<(u32, u32)>,
+ pub max_size: Option<Size>,
/// Whether the window should be visible or not.
pub visible: bool,
@@ -37,12 +63,22 @@ pub struct Settings {
/// Platform specific settings.
pub platform_specific: PlatformSpecific,
+
+ /// Whether the window will close when the user requests it, e.g. when a user presses the
+ /// close button.
+ ///
+ /// This can be useful if you want to have some behavior that executes before the window is
+ /// actually destroyed. If you disable this, you must manually close the window with the
+ /// `window::close` command.
+ ///
+ /// By default this is enabled.
+ pub exit_on_close_request: bool,
}
impl Default for Settings {
- fn default() -> Settings {
- Settings {
- size: (1024, 768),
+ fn default() -> Self {
+ Self {
+ size: Size::new(1024.0, 768.0),
position: Position::default(),
min_size: None,
max_size: None,
@@ -52,25 +88,8 @@ impl Default for Settings {
transparent: false,
level: Level::default(),
icon: None,
+ exit_on_close_request: true,
platform_specific: PlatformSpecific::default(),
}
}
}
-
-impl From<Settings> for iced_winit::settings::Window {
- fn from(settings: Settings) -> Self {
- Self {
- size: settings.size,
- position: iced_winit::Position::from(settings.position),
- min_size: settings.min_size,
- max_size: settings.max_size,
- visible: settings.visible,
- resizable: settings.resizable,
- decorations: settings.decorations,
- transparent: settings.transparent,
- level: settings.level,
- icon: settings.icon.map(Icon::into),
- platform_specific: settings.platform_specific,
- }
- }
-}
diff --git a/src/lib.rs b/src/lib.rs
index cb6e86d4..446590ec 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -168,6 +168,9 @@ use iced_winit::runtime;
pub use iced_futures::futures;
+#[cfg(feature = "highlighter")]
+pub use iced_highlighter as highlighter;
+
mod error;
mod sandbox;
@@ -179,6 +182,9 @@ pub mod window;
#[cfg(feature = "advanced")]
pub mod advanced;
+#[cfg(feature = "multi-window")]
+pub mod multi_window;
+
pub use style::theme;
pub use crate::core::alignment;
@@ -187,7 +193,6 @@ pub use crate::core::{
color, Alignment, Background, BorderRadius, Color, ContentFit, Degrees,
Gradient, Length, Padding, Pixels, Point, Radians, Rectangle, Size, Vector,
};
-pub use crate::runtime::Command;
pub mod clipboard {
//! Access the clipboard.
@@ -225,7 +230,8 @@ pub mod event {
pub mod keyboard {
//! Listen and react to keyboard events.
- pub use crate::core::keyboard::{Event, KeyCode, Modifiers};
+ pub use crate::core::keyboard::key;
+ pub use crate::core::keyboard::{Event, Key, Location, Modifiers};
pub use iced_futures::keyboard::{on_key_press, on_key_release};
}
@@ -236,6 +242,11 @@ pub mod mouse {
};
}
+pub mod command {
+ //! Run asynchronous actions.
+ pub use crate::runtime::command::{channel, Command};
+}
+
pub mod subscription {
//! Listen to external events in your application.
pub use iced_futures::subscription::{
@@ -284,6 +295,7 @@ pub mod widget {
}
pub use application::Application;
+pub use command::Command;
pub use error::Error;
pub use event::Event;
pub use executor::Executor;
diff --git a/src/multi_window.rs b/src/multi_window.rs
new file mode 100644
index 00000000..5b7a00b4
--- /dev/null
+++ b/src/multi_window.rs
@@ -0,0 +1,4 @@
+//! Leverage multi-window support in your application.
+mod application;
+
+pub use application::Application;
diff --git a/src/multi_window/application.rs b/src/multi_window/application.rs
new file mode 100644
index 00000000..4a91bdf4
--- /dev/null
+++ b/src/multi_window/application.rs
@@ -0,0 +1,245 @@
+use crate::style::application::StyleSheet;
+use crate::window;
+use crate::{Command, Element, Executor, Settings, Subscription};
+
+/// 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
+/// [`Command`] in some of its methods. If you do not intend to perform any
+/// background work in your program, the [`Sandbox`] trait offers a simplified
+/// interface.
+///
+/// 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::{Command, 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, Command<Self::Message>) {
+/// (Hello, Command::none())
+/// }
+///
+/// fn title(&self, _window: window::Id) -> String {
+/// String::from("A cool application")
+/// }
+///
+/// fn update(&mut self, _message: Self::Message) -> Command<Self::Message> {
+/// Command::none()
+/// }
+///
+/// fn view(&self, _window: window::Id) -> Element<Self::Message> {
+/// "Hello, world!".into()
+/// }
+/// }
+/// ```
+///
+/// [`Sandbox`]: crate::Sandbox
+pub trait Application: Sized {
+ /// 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 + StyleSheet;
+
+ /// 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 [`Command`] 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, Command<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 [`Command`] returned will be executed immediately in the background.
+ fn update(&mut self, message: Self::Message) -> Command<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, crate::Renderer<Self::Theme>>;
+
+ /// 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) -> <Self::Theme as StyleSheet>::Style {
+ <Self::Theme as StyleSheet>::Style::default()
+ }
+
+ /// 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::renderer::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::renderer::Settings::default()
+ };
+
+ Ok(crate::shell::multi_window::run::<
+ Instance<Self>,
+ Self::Executor,
+ crate::renderer::Compositor<Self::Theme>,
+ >(settings.into(), renderer_settings)?)
+ }
+}
+
+struct Instance<A: Application>(A);
+
+impl<A> crate::runtime::multi_window::Program for Instance<A>
+where
+ A: Application,
+{
+ type Renderer = crate::Renderer<A::Theme>;
+ type Message = A::Message;
+
+ fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
+ self.0.update(message)
+ }
+
+ fn view(
+ &self,
+ window: window::Id,
+ ) -> Element<'_, Self::Message, Self::Renderer> {
+ self.0.view(window)
+ }
+}
+
+impl<A> crate::shell::multi_window::Application for Instance<A>
+where
+ A: Application,
+{
+ type Flags = A::Flags;
+
+ fn new(flags: Self::Flags) -> (Self, Command<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) -> <A::Theme as StyleSheet>::Style {
+ self.0.style()
+ }
+
+ fn subscription(&self) -> Subscription<Self::Message> {
+ self.0.subscription()
+ }
+
+ fn scale_factor(&self, window: window::Id) -> f64 {
+ self.0.scale_factor(window)
+ }
+}
diff --git a/src/settings.rs b/src/settings.rs
index c5e28e86..d9476b61 100644
--- a/src/settings.rs
+++ b/src/settings.rs
@@ -2,6 +2,8 @@
use crate::window;
use crate::{Font, Pixels};
+use std::borrow::Cow;
+
/// The settings of an application.
#[derive(Debug, Clone)]
pub struct Settings<Flags> {
@@ -21,6 +23,9 @@ pub struct Settings<Flags> {
/// [`Application`]: crate::Application
pub flags: Flags,
+ /// The fonts to load on boot.
+ pub fonts: Vec<Cow<'static, [u8]>>,
+
/// The default [`Font`] to be used.
///
/// By default, it uses [`Family::SansSerif`](crate::font::Family::SansSerif).
@@ -41,14 +46,6 @@ pub struct Settings<Flags> {
///
/// [`Canvas`]: crate::widget::Canvas
pub antialiasing: bool,
-
- /// Whether the [`Application`] should exit when the user requests the
- /// window to close (e.g. the user presses the close button).
- ///
- /// By default, it is enabled.
- ///
- /// [`Application`]: crate::Application
- pub exit_on_close_request: bool,
}
impl<Flags> Settings<Flags> {
@@ -62,10 +59,10 @@ impl<Flags> Settings<Flags> {
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,
- exit_on_close_request: default_settings.exit_on_close_request,
}
}
}
@@ -79,10 +76,10 @@ where
id: None,
window: window::Settings::default(),
flags: Default::default(),
+ fonts: Vec::new(),
default_font: Font::default(),
default_text_size: Pixels(16.0),
antialiasing: false,
- exit_on_close_request: true,
}
}
}
@@ -91,9 +88,9 @@ impl<Flags> From<Settings<Flags>> for iced_winit::Settings<Flags> {
fn from(settings: Settings<Flags>) -> iced_winit::Settings<Flags> {
iced_winit::Settings {
id: settings.id,
- window: settings.window.into(),
+ window: settings.window,
flags: settings.flags,
- exit_on_close_request: settings.exit_on_close_request,
+ fonts: settings.fonts,
}
}
}
diff --git a/src/time.rs b/src/time.rs
index 37d454ed..f10f7a5e 100644
--- a/src/time.rs
+++ b/src/time.rs
@@ -1,4 +1,5 @@
//! Listen and react to time.
pub use iced_core::time::{Duration, Instant};
+#[allow(unused_imports)]
pub use iced_futures::backend::default::time::*;
diff --git a/src/window.rs b/src/window.rs
index e4601575..9f96da52 100644
--- a/src/window.rs
+++ b/src/window.rs
@@ -1,12 +1,8 @@
//! Configure the window of your application in native platforms.
-mod position;
-mod settings;
pub mod icon;
pub use icon::Icon;
-pub use position::Position;
-pub use settings::{PlatformSpecific, Settings};
pub use crate::core::window::*;
pub use crate::runtime::window::*;
diff --git a/src/window/position.rs b/src/window/position.rs
deleted file mode 100644
index 6b9fac41..00000000
--- a/src/window/position.rs
+++ /dev/null
@@ -1,32 +0,0 @@
-/// The position of a window in a given screen.
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub enum Position {
- /// The platform-specific default position for a new window.
- Default,
- /// The window is completely centered on the screen.
- Centered,
- /// The window is positioned with specific coordinates: `(X, Y)`.
- ///
- /// When the decorations of the window are enabled, Windows 10 will add some
- /// invisible padding to the window. This padding gets included in the
- /// position. So if you have decorations enabled and want the window to be
- /// at (0, 0) you would have to set the position to
- /// `(PADDING_X, PADDING_Y)`.
- Specific(i32, i32),
-}
-
-impl Default for Position {
- fn default() -> Self {
- Self::Default
- }
-}
-
-impl From<Position> for iced_winit::Position {
- fn from(position: Position) -> Self {
- match position {
- Position::Default => Self::Default,
- Position::Centered => Self::Centered,
- Position::Specific(x, y) => Self::Specific(x, y),
- }
- }
-}