Improve setting background

This commit is contained in:
hodasemi 2023-01-19 13:05:48 +01:00
parent ef9d140894
commit 672c0f00ba
7 changed files with 74 additions and 77 deletions

View file

@ -145,6 +145,21 @@ impl FillType {
}
}
impl PartialEq<FillTypeInfo> for FillType {
fn eq(&self, info: &FillTypeInfo) -> bool {
match (&self.inner, info) {
(InnerFillType::Image(inner_image), FillTypeInfo::Image(info_image)) => {
inner_image.path() == info_image
}
(InnerFillType::Color(inner_color), FillTypeInfo::Color(info_color)) => {
inner_color.color() == *info_color
}
_ => false,
}
}
}
impl Drop for FillType {
fn drop(&mut self) {
if self.visible.load(SeqCst) {

View file

@ -149,19 +149,7 @@ impl Grid {
}
pub fn set_background(&self, background: impl Into<FillTypeInfo>) -> Result<()> {
let background = FillType::new(self.framable.clone(), background.into())?;
if self.framable.is_framed() {
background.update_frame()?;
}
if self.visible.load(SeqCst) {
background.enable()?;
}
*self.background.write().unwrap() = Some(background);
Ok(())
super::set_background(self.visible(), &self.framable, &self.background, background)
}
pub(crate) fn background(&self) -> RwLockReadGuard<'_, Option<FillType>> {

View file

@ -126,19 +126,7 @@ impl Icon {
}
pub fn set_background(&self, background: impl Into<FillTypeInfo>) -> Result<()> {
let background = FillType::new(self.framable.clone(), background.into())?;
if self.framable.is_framed() {
background.update_frame()?;
}
if self.visible() {
background.enable()?;
}
*self.background.write().unwrap() = Some(background);
Ok(())
super::set_background(self.visible(), &self.framable, &self.background, background)
}
pub fn clear_background(&self) {

View file

@ -124,10 +124,7 @@ impl Label {
}
pub fn set_background(&self, background: impl Into<FillTypeInfo>) -> Result<()> {
*self.background.write().unwrap() =
Some(FillType::new(self.framable.clone(), background.into())?);
Ok(())
super::set_background(self.visible(), &self.framable, &self.background, background)
}
pub fn try_from(label_info: &LabelInfo, gui_handler: &Arc<GuiHandler>) -> Result<Arc<Self>> {

View file

@ -2,11 +2,13 @@ use crate::{guihandler::gui::iconizable::IconBuilderType, prelude::*};
use anyhow::Result;
use std::sync::{
atomic::{AtomicU32, Ordering::SeqCst},
Arc, Mutex,
Arc, Mutex, RwLock,
};
use utilities::prelude::*;
use vulkan_rs::prelude::*;
use self::fill_type::FillType;
pub mod button;
pub mod fill_type;
pub mod grid;
@ -322,3 +324,35 @@ impl Drop for IconizableWrapper {
}
}
}
pub(crate) fn set_background(
visible: bool,
framable: &Arc<Framable>,
current: &RwLock<Option<FillType>>,
new: impl Into<FillTypeInfo>,
) -> Result<()> {
let mut current_background = current.write().unwrap();
let fill_info = new.into();
let update = match &*current_background {
Some(bg) => *bg != fill_info,
None => true,
};
if update {
let fill_type = FillType::new(framable.clone(), fill_info)?;
if framable.is_framed() {
fill_type.update_frame()?;
}
if visible {
fill_type.enable()?;
}
*current_background = Some(fill_type);
}
Ok(())
}

View file

@ -4,7 +4,7 @@ use utilities::prelude::*;
use std::sync::{
atomic::{AtomicBool, Ordering::SeqCst},
Arc, Mutex,
Arc, RwLock,
};
use super::fill_type::{FillType, InnerFillType};
@ -51,7 +51,7 @@ impl TextFieldBuilder {
pub fn build(self, gui_handler: Arc<GuiHandler>) -> Result<Arc<TextField>> {
let framable = Framable::new(gui_handler.clone(), false)?;
let background = Mutex::new(
let background = RwLock::new(
self.background
.clone()
.map(|info| FillType::new(framable.clone(), info))
@ -101,7 +101,7 @@ pub struct TextField {
text_changed_executable: Arc<Executable<Option<String>>>,
background: Mutex<Option<FillType>>,
background: RwLock<Option<FillType>>,
visible: AtomicBool,
}
@ -141,7 +141,7 @@ impl TextField {
}
fn update_color_change(self: &Arc<Self>) {
if let Some(background) = &*self.background.lock().unwrap() {
if let Some(background) = &*self.background.read().unwrap() {
if let InnerFillType::Color(colorable) = &background.inner {
let normal_color = colorable.color();
let focus_color = normal_color * 1.4;
@ -151,12 +151,12 @@ impl TextField {
self.writeable.set_activation_changed(move |active| {
if let Some(me) = weak_self.upgrade() {
if active {
if let Some(background) = &*me.background.lock().unwrap() {
if let Some(background) = &*me.background.read().unwrap() {
if let InnerFillType::Color(colorable) = &background.inner {
colorable.set_color(focus_color)?;
}
}
} else if let Some(background) = &*me.background.lock().unwrap() {
} else if let Some(background) = &*me.background.read().unwrap() {
if let InnerFillType::Color(colorable) = &background.inner {
colorable.set_color(normal_color)?;
}
@ -195,40 +195,7 @@ impl TextField {
}
pub fn set_background(self: &Arc<Self>, background: impl Into<FillTypeInfo>) -> Result<()> {
{
let mut current_background = self.background.lock().unwrap();
let new_background: FillTypeInfo = background.into();
match current_background.as_mut() {
Some(current_background) => match (&current_background.inner, &new_background) {
(InnerFillType::Image(displayable), FillTypeInfo::Image(path)) => {
displayable.set_image(path.clone())?;
}
(InnerFillType::Color(colorable), FillTypeInfo::Color(new_color)) => {
colorable.set_color(*new_color)?;
}
_ => {
let ft = FillType::new(self.framable.clone(), new_background)?;
if self.visible() {
ft.enable()?;
}
*current_background = ft;
}
},
None => {
let ft = FillType::new(self.framable.clone(), new_background)?;
if self.visible() {
ft.enable()?;
}
*current_background = Some(ft);
}
}
}
super::set_background(self.visible(), &self.framable, &self.background, background)?;
self.update_color_change();
@ -236,7 +203,7 @@ impl TextField {
}
pub fn background_color(&self) -> Option<Color> {
if let Some(background) = self.background.lock().unwrap().as_ref() {
if let Some(background) = self.background.read().unwrap().as_ref() {
if let InnerFillType::Color(colorable) = &background.inner {
return Some(colorable.color());
}
@ -262,7 +229,7 @@ impl TextField {
self.textable.delete()?;
self.writeable.delete()?;
if let Some(background) = self.background.lock().unwrap().as_ref() {
if let Some(background) = self.background.read().unwrap().as_ref() {
background.disable()?;
}
@ -298,7 +265,7 @@ impl Gridable for TextField {
self.textable.update_text()?;
if let Some(background) = self.background.lock().unwrap().as_ref() {
if let Some(background) = self.background.read().unwrap().as_ref() {
background.update_frame()?;
}
@ -320,7 +287,7 @@ impl Gridable for TextField {
self.textable.set_ui_layer(layer);
self.writeable.set_ui_layer(layer);
if let Some(background) = self.background.lock().unwrap().as_ref() {
if let Some(background) = self.background.read().unwrap().as_ref() {
background.set_ui_layer(layer);
}
@ -342,7 +309,7 @@ impl Visibility for TextField {
self.textable.add()?;
self.writeable.add()?;
if let Some(background) = self.background.lock().unwrap().as_ref() {
if let Some(background) = self.background.read().unwrap().as_ref() {
background.enable()?;
}
} else {

View file

@ -23,6 +23,8 @@ pub struct Displayable {
buffer: Arc<Buffer<TexturedVertex>>,
path: AssetPath,
ui_layer: AtomicI32,
left_factor: RwLock<f32>,
@ -44,7 +46,7 @@ impl Displayable {
/// * `framable` is a `Arc<Framable>` instance
/// * `name` is the name for a png
pub fn new(framable: Arc<Framable>, name: AssetPath) -> Result<Arc<Self>> {
let descriptor_set = framable.gui_handler().image_descriptor(name)?;
let descriptor_set = framable.gui_handler().image_descriptor(name.clone())?;
let buffer = Buffer::builder()
.set_usage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT)
@ -57,6 +59,8 @@ impl Displayable {
descriptor_set: RwLock::new(descriptor_set),
buffer,
path: name,
ui_layer: AtomicI32::new(0),
left_factor: RwLock::new(0.0),
@ -103,6 +107,10 @@ impl Displayable {
.delete_displayable(self.ui_layer.load(SeqCst), self)
}
pub fn path(&self) -> &AssetPath {
&self.path
}
pub fn set_ui_layer(&self, ui_layer: i32) {
self.ui_layer.store(ui_layer, SeqCst);
}