Skip to main content

iced_core/
settings.rs

1//! Configure your application.
2use crate::backend;
3use crate::renderer;
4use crate::text;
5use crate::{Backend, Font, Pixels};
6
7use std::borrow::Cow;
8
9/// The settings of an iced program.
10#[derive(Debug, Clone)]
11pub struct Settings {
12    /// The identifier of the application.
13    ///
14    /// If provided, this identifier may be used to identify the application or
15    /// communicate with it through the windowing system.
16    pub id: Option<String>,
17
18    /// The fonts to load on boot.
19    pub fonts: Vec<Cow<'static, [u8]>>,
20
21    /// The default [`Font`] to be used.
22    ///
23    /// By default, it uses [`Family::SansSerif`](crate::font::Family::SansSerif).
24    pub font: Font,
25
26    /// The text size that will be used by default.
27    ///
28    /// By default, it is `16.0`.
29    pub text_size: Pixels,
30
31    /// The default line height of text.
32    ///
33    /// By default, it is `LineHeight::Relative(1.375)`.
34    pub line_height: text::LineHeight,
35
36    /// Whether certain widgets should be rendered using metrics hinting.
37    ///
38    /// Metrics hinting can improve the readability of smaller text in
39    /// low-DPI screens, as well as the clarity of widgets that render thin lines.
40    ///
41    /// By default, it is enabled.
42    pub metrics_hinting: bool,
43
44    /// The graphical backend to use.
45    ///
46    /// By default, it is [`Backend::Best`].
47    pub backend: Backend,
48
49    /// The [`PowerPreference`](backend::PowerPreference) of the backend.
50    ///
51    /// By default, it is [`backend::PowerPreference::None`].
52    pub power_preference: backend::PowerPreference,
53
54    /// If set to true, the renderer will try to perform antialiasing for some
55    /// primitives.
56    ///
57    /// Enabling it can produce a smoother result in some widgets, like the
58    /// `canvas` widget, at a performance cost.
59    ///
60    /// By default, it is enabled.
61    pub antialiasing: bool,
62
63    /// Whether or not to attempt to synchronize rendering when possible.
64    ///
65    /// Disabling it can improve rendering performance on some platforms.
66    ///
67    /// By default, it is enabled.
68    pub vsync: bool,
69}
70
71impl Default for Settings {
72    fn default() -> Self {
73        let renderer = renderer::Settings::default();
74
75        Self {
76            id: None,
77            fonts: Vec::new(),
78            font: renderer.font,
79            text_size: renderer.text_size,
80            line_height: renderer.line_height,
81            metrics_hinting: true,
82            backend: Backend::default(),
83            power_preference: backend::PowerPreference::None,
84            antialiasing: true,
85            vsync: true,
86        }
87    }
88}
89
90impl From<&Settings> for renderer::Settings {
91    fn from(settings: &Settings) -> Self {
92        Self {
93            font: settings.font,
94            text_size: settings.text_size,
95            line_height: settings.line_height,
96            metrics_hinting: settings.metrics_hinting,
97        }
98    }
99}