Container width and height not in pixels? #1008
-
Hello, fn view(&mut self) -> Element<Message> {
[...]
let img = iced::image::Image::new(iced::image::Handle::from_pixels(
200,
200,
pixels, // all pixels are black except for one at (100, 100) that is white
));
Container::new(
Container::new(img)
.max_width(200 as u32)
.max_height(200 as u32)
.width(Length::Fill)
.height(Length::Fill)
)
.width(Length::Fill)
.height(Length::Fill)
.style(Style) // blue background
.into()
} The documentation explicitly states that max_width and max_height take max container size in pixels, so I'm confused why the image turns out to be 222x222, and as a result instead of one white pixel we are seeing four gray ones. OS: Debian 10, x86_64 Full codeuse iced::{
container, executor, Application, Clipboard, Color, Column, Command, Container, Element,
Length, Settings, Subscription,
};
const MAP_SIZE: usize = 200;
pub fn main() -> iced::Result {
App::run(Settings {
antialiasing: true,
..Settings::default()
})
}
struct App {
map: Map,
}
type Message = String;
pub struct Style;
impl container::StyleSheet for Style {
fn style(&self) -> container::Style {
container::Style {
background: Color::from_rgb8(0x0, 0x0, 0xa0).into(),
..container::Style::default()
}
}
}
impl Application for App {
type Executor = executor::Default;
type Message = Message;
type Flags = ();
fn new(_flags: ()) -> (Self, Command<Message>) {
(
App {
map: Map::default(),
},
Command::none(),
)
}
fn title(&self) -> String {
String::from("Demo")
}
fn update(&mut self, _message: Message, _clipboard: &mut Clipboard) -> Command<Message> {
//self.map.iteration();
Command::none()
}
fn subscription(&self) -> Subscription<Message> {
iced::Subscription::none()
}
fn view(&mut self) -> Element<Message> {
self.map.c(100, 100).height = 1.0;
let pixels = self.map.to_pixels();
let img = iced::image::Image::new(iced::image::Handle::from_pixels(
MAP_SIZE as u32,
MAP_SIZE as u32,
pixels,
));
Container::new(
Container::new(img)
.max_width(MAP_SIZE as u32)
.max_height(MAP_SIZE as u32)
.width(Length::Fill)
.height(Length::Fill)
)
.width(Length::Fill)
.height(Length::Fill)
.style(Style) // blue background
.into()
}
}
#[derive(Clone, Copy)]
struct MapCell {
height: f32,
}
impl Default for MapCell {
fn default() -> MapCell {
MapCell { height: 0.0 }
}
}
impl MapCell {
fn to_color(&self) -> (u8, u8, u8) {
(
(self.height * 255.0) as u8,
(self.height * 255.0) as u8,
(self.height * 255.0) as u8,
)
}
}
struct Map {
cells: Vec<MapCell>,
}
impl Default for Map {
fn default() -> Map {
Map {
cells: vec![MapCell::default(); MAP_SIZE * MAP_SIZE],
}
}
}
impl Map {
pub fn c(&mut self, x: usize, y: usize) -> &mut MapCell {
&mut self.cells[y * MAP_SIZE + x]
}
fn to_pixels(&mut self) -> Vec<u8> {
let mut pixels = vec![128; MAP_SIZE * MAP_SIZE * 4];
for y in 0..MAP_SIZE {
for x in 0..MAP_SIZE {
let color = self.c(x, y).to_color();
pixels[(y * MAP_SIZE + x) * 4 + 0] = color.0;
pixels[(y * MAP_SIZE + x) * 4 + 1] = color.1;
pixels[(y * MAP_SIZE + x) * 4 + 2] = color.2;
pixels[(y * MAP_SIZE + x) * 4 + 3] = 0xff;
}
}
pixels
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This scale factor is determined by the display specifications (DPI) and the OS GUI scaling settings. Therefore, logical pixels will not necessarily map 1:1 to physical pixels on every environment. We only allow using logical pixels because this way every |
Beta Was this translation helpful? Give feedback.
-
Is there a way to get the screen resolution in logical pixels? I would like to set as initial window dimensions full-screen, independently from the system the application is running on. |
Beta Was this translation helpful? Give feedback.
iced
length units represent logical pixels. Logical pixels are transformed to physical pixels using a scale factor.This scale factor is determined by the display specifications (DPI) and the OS GUI scaling settings. Therefore, logical pixels will not necessarily map 1:1 to physical pixels on every environment.
We only allow using logical pixels because this way every
iced
application is able to adapt to any display while respecting the OS scaling settings, all without burdening the user with the implementation details.