This commit is contained in:
TimUntersberger 2021-06-25 18:03:18 +02:00
parent 5c45d36d1a
commit de79a01b88
3 changed files with 41 additions and 44 deletions

View file

@ -1,65 +1,46 @@
use iced::{button, Align, Button, Column, Element, Sandbox, Settings, window::Settings as WindowSettings, Text};
use iced::{Column, Element, Sandbox, Settings, window::Settings as WindowSettings};
const WINDOW_WIDTH: i32 = 200;
const WINDOW_HEIGHT: i32 = 200;
const DISPLAY_WIDTH: i32 = 1920;
const DISPLAY_HEIGHT: i32 = 1080;
// These numbers are specific to a 1920x1080 monitor
const BORDER_X: i32 = 8;
const BORDER_Y: i32 = 2;
const CAPTION_HEIGHT: i32 = 4;
pub fn main() {
let x = DISPLAY_WIDTH / 2 - WINDOW_WIDTH / 2 - BORDER_X;
let y = DISPLAY_HEIGHT / 2 - WINDOW_HEIGHT / 2 - BORDER_Y - CAPTION_HEIGHT;
let settings = Settings {
window: WindowSettings {
size: (400, 200),
position: (100, 100),
size: (WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32),
position: (x, y),
..Default::default()
},
..Default::default()
};
Counter::run(settings).unwrap()
Winit::run(settings).unwrap()
}
#[derive(Default)]
struct Counter {
value: i32,
increment_button: button::State,
decrement_button: button::State,
}
struct Winit;
#[derive(Debug, Clone, Copy)]
enum Message {
IncrementPressed,
DecrementPressed,
}
impl Sandbox for Counter {
type Message = Message;
impl Sandbox for Winit {
type Message = ();
fn new() -> Self {
Self::default()
}
fn title(&self) -> String {
String::from("Counter with winit - Iced")
String::from("winit - Iced")
}
fn update(&mut self, message: Message) {
match message {
Message::IncrementPressed => {
self.value += 1;
}
Message::DecrementPressed => {
self.value -= 1;
}
}
fn update(&mut self, _message: Self::Message) {
}
fn view(&mut self) -> Element<Message> {
Column::new()
.padding(20)
.align_items(Align::Center)
.push(
Button::new(&mut self.increment_button, Text::new("Increment"))
.on_press(Message::IncrementPressed),
)
.push(Text::new(self.value.to_string()).size(50))
.push(
Button::new(&mut self.decrement_button, Text::new("Decrement"))
.on_press(Message::DecrementPressed),
)
.into()
fn view(&mut self) -> Element<Self::Message> {
Column::new().into()
}
}

View file

@ -8,8 +8,23 @@ pub struct Settings {
/// The initial position of the window.
///
/// When the decorations of the window are enabled, Windows 10 will add some inivisble 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
/// -DPI_BORDER_X,-DPI_BORDER_Y.
///
/// DPI_BORDER_X/DPI_BORDER_Y are the usual size of the padding, which changes based on the DPI of the display.
///
/// On a 1920x1080 monitor you would have to set the position to -8,-2.
///
/// For info on how you could implement positioning that supports all DPI monitors look at the
/// following WINAPI calls:
///
/// * GetDpiForMonitor (with MDT_RAW_DPI)
/// * GetSystemMetricsForDpi (with SM_CXFRAME and SM_CYFRAME)
///
/// Note: this gets ignored on the web
pub position: (u32, u32),
pub position: (i32, i32),
/// The minimum size of the window.
pub min_size: Option<(u32, u32)>,

View file

@ -36,7 +36,7 @@ pub struct Window {
pub size: (u32, u32),
/// The position of the window.
pub position: (u32, u32),
pub position: (i32, i32),
/// The minimum size of the window.
pub min_size: Option<(u32, u32)>,
@ -78,7 +78,7 @@ impl Window {
window_builder = window_builder
.with_title(title)
.with_inner_size(winit::dpi::LogicalSize { width, height })
.with_outer_position(self.position)
.with_position(winit::dpi::LogicalPosition { x: self.position.0, y: self.position.1 })
.with_resizable(self.resizable)
.with_decorations(self.decorations)
.with_transparent(self.transparent)
@ -116,6 +116,7 @@ impl Default for Window {
fn default() -> Window {
Window {
size: (1024, 768),
position: (100, 100),
min_size: None,
max_size: None,
resizable: true,