Added exclusive video mode selection in window example.

This commit is contained in:
Samuel Guerra 2023-09-03 16:04:52 -03:00
parent dbfa79bdc7
commit 7ac3933e99
3 changed files with 76 additions and 7 deletions

View File

@ -1,6 +1,6 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use zero_ui::core::app::EXIT_CMD;
use zero_ui::core::units::{DipPoint, DipSize};
use zero_ui::core::units::{DipPoint, DipSize, Factor};
use zero_ui::prelude::new_widget::WINDOW;
use zero_ui::prelude::*;
@ -29,7 +29,10 @@ async fn main_window() -> WindowRoot {
let title = merge_var!(
window_vars.actual_position(),
window_vars.actual_size(),
move |p: &DipPoint, s: &DipSize| { formatx!("Window Example - position: {p:.0?}, size: {s:.0?}") }
window_vars.scale_factor(),
move |p: &DipPoint, s: &DipSize, f: &Factor| {
formatx!("Window Example - position: {p:.0?}, size: {s:.0?}, factor: {f:?}")
}
);
let background = var(colors::BLACK);
@ -358,11 +361,45 @@ fn state() -> impl UiNode {
separator(),
state_btn(WindowState::Fullscreen),
state_btn(WindowState::Exclusive),
Text!("TODO video mode"),
exclusive_mode(),
],
)
}
fn exclusive_mode() -> impl UiNode {
Toggle! {
style_fn = toggle::ComboStyle!();
tooltip = Tip!(Text!("Exclusive video mode"));
child = Text! {
txt = WINDOW.vars().video_mode().map_to_text();
txt_align = Align::CENTER;
padding = 2;
};
checked_popup = wgt_fn!(|_| {
let vars = WINDOW.vars();
let selected_opt = vars.video_mode();
let default_opt = zero_ui::core::window::VideoMode::MAX;
let opts = vars.video_modes().get();
popup::Popup! {
child = Scroll! {
mode = ScrollMode::VERTICAL;
child = Stack! {
toggle::selector = toggle::Selector::single(selected_opt);
direction = StackDirection::top_to_bottom();
children = [default_opt].into_iter().chain(opts).map(|o| Toggle! {
child = Text!(formatx!("{o}"));
value = o;
})
.collect::<UiNodeVec>();
}
};
}
});
}
}
fn visibility() -> impl UiNode {
let visible = WINDOW.vars().visible();
let btn = Button! {

View File

@ -247,6 +247,18 @@ impl WindowVars {
self.0.actual_monitor.read_only()
}
/// Available video modes in the current monitor.
pub fn video_modes(&self) -> BoxedVar<Vec<VideoMode>> {
self.0
.actual_monitor
.flat_map(|&m| {
m.and_then(|m| super::MONITORS.monitor(m))
.unwrap_or_else(super::MonitorInfo::fallback)
.video_modes()
})
.boxed()
}
/// Current scale factor of the current monitor hosting the window.
pub fn scale_factor(&self) -> ReadOnlyArcVar<Factor> {
self.0.scale_factor.read_only()

View File

@ -3700,10 +3700,30 @@ pub struct VideoMode {
}
impl Default for VideoMode {
fn default() -> Self {
Self {
size: PxSize::new(Px::MAX, Px::MAX),
bit_depth: u16::MAX,
refresh_rate: u32::MAX,
Self::MAX
}
}
impl VideoMode {
/// Default value, matches with the largest size, greatest bit-depth and refresh rate.
pub const MAX: VideoMode = VideoMode {
size: PxSize::new(Px::MAX, Px::MAX),
bit_depth: u16::MAX,
refresh_rate: u32::MAX,
};
}
impl fmt::Display for VideoMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if *self == Self::MAX {
write!(f, "MAX")
} else {
write!(
f,
"{}x{}, {}, {}hz",
self.size.width.0,
self.size.height.0,
self.bit_depth,
(self.refresh_rate as f32 * 0.001).round()
)
}
}
}