Store and synchronize Menu in application::State

This commit is contained in:
Héctor Ramón Jiménez 2021-07-12 22:28:18 +02:00
parent b3ff522c18
commit 31997d255f
No known key found for this signature in database
GPG key ID: 44B88EB52AB1EE8D
5 changed files with 64 additions and 13 deletions

View file

@ -1,5 +1,5 @@
use crate::conversion;
use crate::{Application, Color, Debug, Mode, Point, Size, Viewport};
use crate::{Application, Color, Debug, Menu, Mode, Point, Size, Viewport};
use std::marker::PhantomData;
use winit::event::{Touch, WindowEvent};
@ -9,6 +9,7 @@ use winit::window::Window;
#[derive(Debug, Clone)]
pub struct State<A: Application> {
title: String,
menu: Menu<A::Message>,
mode: Mode,
background_color: Color,
scale_factor: f64,
@ -23,6 +24,7 @@ impl<A: Application> State<A> {
/// Creates a new [`State`] for the provided [`Application`] and window.
pub fn new(application: &A, window: &Window) -> Self {
let title = application.title();
let menu = application.menu();
let mode = application.mode();
let background_color = application.background_color();
let scale_factor = application.scale_factor();
@ -36,10 +38,9 @@ impl<A: Application> State<A> {
)
};
window.set_menu(Some(conversion::menu(application.menu())));
Self {
title,
menu,
mode,
background_color,
scale_factor,
@ -52,6 +53,11 @@ impl<A: Application> State<A> {
}
}
/// Returns the current [`Menu`] of the [`State`].
pub fn menu(&self) -> &Menu<A::Message> {
&self.menu
}
/// Returns the current background [`Color`] of the [`State`].
pub fn background_color(&self) -> Color {
self.background_color
@ -205,5 +211,14 @@ impl<A: Application> State<A> {
self.scale_factor = new_scale_factor;
}
// Update menu
let new_menu = application.menu();
if self.menu != new_menu {
window.set_menu(Some(conversion::menu(&new_menu)));
self.menu = new_menu;
}
}
}