1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#[doc(hidden)]
#[cfg(target_os = "linux")]
mod implz {
	use uniui_gui::prelude::*;
	uniui_gui::desktop_qt! {
		extern "C" {
			pub fn uniui_wrapper_minimalsize_set_for(widget: *mut std::ffi::c_void);
		}
	}

	pub fn set_minimal(
		w: NativeWidget,
		_: &mut dyn WidgetGenerator,
	) -> Result<NativeWidget, ()> {
		uniui_gui::desktop_qt! {
			unsafe {
				uniui_wrapper_minimalsize_set_for(w);
			};
		}

		uniui_gui::desktop_noop! {
			// nothing to do
		}

		return Ok(w);
	}
}

#[doc(hidden)]
#[cfg(target_os = "android")]
extern crate uni_tmp_jni as jni;

use uniui_gui::prelude::*;

/// Contains [MinimalSize] and [u_minimalsize!]
pub mod prelude {
	pub use crate::{
		u_minimalsize,
		MinimalSize,
	};
}

#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
mod implz {
	use uniui_gui::prelude::*;

	pub fn set_minimal(
		w: NativeWidget,
		_: &mut dyn WidgetGenerator,
	) -> Result<NativeWidget, ()> {
		w.style().set_property_silent("flex-grow", "0");
		return Ok(w);
	}
}

#[doc(hidden)]
#[cfg(target_os = "android")]
mod implz {
	mod android;
	pub use android::set_minimal;
}


/// Simplifies [MinimalSize] creation.
///
/// ```
/// let minimal_widget = u_minimalsize!(widget);
/// ```
#[macro_export]
macro_rules! u_minimalsize {
	($w:expr) => {{
		$crate::MinimalSize {
			widget: $w,
			}
		}};
}


/// Force widget to be as small as possible.
///
/// Consider using [u_minimalsize!](u_minimalsize!) for simplification.
///
/// Example:
/// ```
/// let nonexpandable_widget = MinimalSize {
///     widget: expandable_widget,
/// };
/// ```
pub struct MinimalSize<T: Widget> {
	pub widget: T,
}


impl<T: Widget> Widget for MinimalSize<T> {
	fn to_native(
		&mut self,
		widget_generator: &mut dyn WidgetGenerator,
	) -> Result<NativeWidget, ()> {
		let result = self.widget.to_native(widget_generator)?;
		return implz::set_minimal(result, widget_generator);
	}

	fn draw(&mut self) {
		self.widget.draw();
	}
}

impl<T: Widget> DataProcessor for MinimalSize<T> {
	fn process_data(
		&mut self,
		msec_since_app_start: i64,
		app: &mut dyn Application,
	) {
		self.widget.process_data(msec_since_app_start, app);
	}
}