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
use crate::TextAlignment;

use super::implz::Platform;

use uniui_gui::prelude::*;

/// Shows text on the screen.
///
/// ```
/// let label = Label::new("Hello world".to_owned());
/// lable.slot_set_text_alignment().exec_for(TextAlignment::Center);
/// ```
///
/// # Macros
///
/// You can use [u_label!] to simplify Label creation/setup:
/// ```
/// let label = u_label! {
///     text: "Hello world!".to_owned(),
///     text_alignment: TextAlignment::Center,
/// };
/// ```
pub struct Label {
	text: String,
	platform: Platform,
	slot_set_text: SlotImpl<String>,
	text_alignment: Property<TextAlignment>,
}

impl Label {
	/// Creates new Label with provided text. You may be interested in [u_label!] to
	/// simplify Label creation/setup.
	///
	/// ```
	/// let label = Label::new("Hello".to_owned());
	/// ```
	pub fn new(text: String) -> Label {
		return Label {
			text,
			platform: Platform::new(),
			slot_set_text: SlotImpl::new(),
			text_alignment: Property::new(TextAlignment::Start),
		};
	}

	/// Label's text can be changed via provided slot
	///
	/// ```
	/// let label = u_label!{
	///     text: "Hello".to_owned(),
	/// };
	///
	/// let mut signal = Signal::new();
	/// signal.connect_slot(label.slot_set_text);
	///
	/// // <...>
	///
	/// // Changes label's text to "Goodbye"
	/// signal.emit("Goodbye".to_owned());
	/// ```
	pub fn slot_set_text(&self) -> &dyn Slot<String> {
		return &self.slot_set_text;
	}

	/// Label's text alignment can be changed via provided slot
	///
	/// ```
	/// let label = u_lable!{
	/// 	text: "Hello".to_owned(),
	/// };
	///
	/// let mut signal = Signal::new();
	/// signal.connect_slot(label.slot_set_text_alignment());
	///
	/// // <...>
	///
	/// // Changes text alignemnt
	/// signal.emit(TextAligment::Center);
	/// ```
	pub fn slot_set_text_alignment(&self) -> &dyn Slot<TextAlignment> {
		return self.text_alignment.slot();
	}

	fn set_text(
		&mut self,
		s: String,
	) {
		self.text = s;
		self.platform.set_text(&self.text);
	}
}

impl Widget for Label {
	fn to_native(
		&mut self,
		widget_generator: &mut dyn WidgetGenerator,
	) -> Result<NativeWidget, ()> {
		return self.platform.to_native(widget_generator, &self.text);
	}
}

impl DataProcessor for Label {
	fn process_data(
		&mut self,
		_: i64,
		_: &mut dyn Application,
	) {
		if let Some(text) = self.slot_set_text.data_iter().last() {
			self.set_text(text);
		}

		if self.text_alignment.try_update() {
			self.platform.update_alignment(self.text_alignment.as_ref());
		}
	}
}