use super::*;
impl AppRuntime {
#[inline]
pub fn reset_caret_blink(&mut self) {
self.caret_blink_epoch = std::time::Instant::now();
self.last_caret_blink_state = true;
}
pub fn poll_caret_blink(&mut self) -> bool {
if self.active_focus.is_none() {
return false;
}
let elapsed_ms = self.caret_blink_epoch.elapsed().as_millis();
let is_blink_on = (elapsed_ms / 530).is_multiple_of(2);
if is_blink_on != self.last_caret_blink_state {
self.last_caret_blink_state = is_blink_on;
true
} else {
false
}
}
pub(crate) fn input_is_password(
&self,
input_index: usize,
input_id: Option<&str>,
entity_id: Option<strata_primitives::EntityId>,
) -> bool {
let mut bp_counter = 0;
let bp = entity_id
.and_then(|eid| self.root_blueprint.find_by_entity_id(eid))
.or_else(|| {
Self::find_input_blueprint(
&self.root_blueprint,
input_index,
input_id,
&mut bp_counter,
)
});
matches!(
bp,
Some(NodeBlueprint::Element { tag: Tag::Input, attributes, .. })
if Self::is_password_input_attrs(attributes)
)
}
pub(crate) fn active_input_is_password(&self, active: &ActiveInput) -> bool {
self.input_is_password(
active.input_index,
active.input_id.as_deref(),
active.entity_id,
)
}
pub fn sync_active_input_layout(&mut self) {
let Some(ref active) = self.active_focus else {
return;
};
let target_idx = active.input_index;
let target_id = active.input_id.clone();
let target_entity_id = active.entity_id;
let is_password =
self.input_is_password(target_idx, target_id.as_deref(), target_entity_id);
let mut counter = 0;
let text_val = active.state.text.get();
let layout_by_entity = if let Some(eid) = target_entity_id {
self.layout_root.find_by_entity_id_mut(eid)
} else {
None
};
let layout_match = match layout_by_entity {
Some(l) => Some(l),
None => Self::find_input_layout_mut(
&self.root_blueprint,
&mut self.layout_root,
target_idx,
target_id.as_deref(),
&mut counter,
),
};
if let Some(layout) = layout_match {
let is_mono = strata_layout::is_monospace_family(layout.style.font_family);
if !text_val.is_empty() {
let display_val = if is_password {
Self::mask_password_text(&text_val)
} else {
text_val
};
layout.text = Some(display_val.clone());
if let Some(c) = layout.style.caret_color {
layout.style.color = c;
}
let (tw, th) = strata_layout::text::measure_text_bounds_with_family(
&display_val,
layout.style.font_size,
layout.style.line_height,
None,
is_mono,
layout.style.font_family,
);
layout.geometry.content_box.width = tw.max(layout.geometry.content_box.width);
layout.geometry.content_box.height = th.max(layout.geometry.content_box.height);
} else {
let mut ph = None;
let mut bp_counter = 0;
let bp_match = if let Some(eid) = target_entity_id {
self.root_blueprint.find_by_entity_id(eid)
} else {
None
};
let bp_match = bp_match.or_else(|| {
Self::find_input_blueprint(
&self.root_blueprint,
target_idx,
target_id.as_deref(),
&mut bp_counter,
)
});
if let Some(bp) = bp_match {
if let NodeBlueprint::Element { attributes, .. } = bp {
for attr in attributes {
if let strata_dom::Attribute::Property { key, value } = attr {
if key == "placeholder" && !value.is_empty() {
ph = Some(value.clone());
}
}
}
}
}
if let Some(ph_str) = ph {
layout.text = Some(ph_str);
layout.style.color = strata_style::types::Color::new(140, 145, 155, 255);
} else {
layout.text = Some(String::new());
}
}
}
self.update_gtk_ime_location();
}
pub fn calculate_exact_char_index_at_offset(
text: &str,
rel_x: f32,
font_size: f32,
line_height: f32,
is_mono: bool,
font_family: strata_primitives::AtomId,
) -> usize {
if rel_x <= 0.0 || text.is_empty() {
return 0;
}
let chars: Vec<char> = text.chars().collect();
let total = chars.len();
let (full_w, _) = strata_layout::text::measure_text_bounds_with_family(
text,
font_size,
line_height,
None,
is_mono,
font_family,
);
if rel_x >= full_w {
return total;
}
let mut prev_w = 0.0f32;
for i in 1..=total {
let prefix: String = chars[..i].iter().collect();
let (w, _) = strata_layout::text::measure_text_bounds_with_family(
&prefix,
font_size,
line_height,
None,
is_mono,
font_family,
);
let mid = (prev_w + w) / 2.0;
if rel_x < mid {
return i - 1;
} else if rel_x <= w {
return i;
}
prev_w = w;
}
total
}
pub fn render_active_input_caret(&mut self) {
let Some(ref active) = self.active_focus else {
return;
};
let mut current_idx = 0;
let target_idx = active.input_index;
let target_id = active.input_id.as_deref();
let is_password = self.input_is_password(target_idx, target_id, active.entity_id);
let layout_by_entity = if let Some(eid) = active.entity_id {
self.layout_root.find_by_entity_id(eid)
} else {
None
};
let Some(layout) = layout_by_entity.or_else(|| {
Self::find_input_layout(
&self.root_blueprint,
&self.layout_root,
target_idx,
target_id,
&mut current_idx,
)
}) else {
return;
};
let content_box = layout.geometry.content_box;
if content_box.width <= 0.0 || content_box.height <= 0.0 {
return;
}
let font_size = layout.style.font_size;
let line_height = layout.style.line_height;
let is_mono = strata_layout::is_monospace_family(layout.style.font_family);
let font_family = layout.style.font_family;
let text_color = layout.style.color;
let mut caret_color = layout.style.caret_color.unwrap_or(text_color);
if caret_color.a == 0 {
caret_color = strata_style::types::Color::new(236, 233, 225, 255);
}
let raw_text = active.state.text.get();
let text = if is_password {
Self::mask_password_text(&raw_text)
} else {
raw_text
};
let total_chars = text.chars().count();
let cursor_idx = active.state.cursor_index.get().min(total_chars);
let align_offset = match layout.style.text_align {
strata_style::types::TextAlign::Center => {
let (tw, _) = strata_layout::text::measure_text_bounds_with_family(
&text,
font_size,
line_height,
None,
is_mono,
font_family,
);
((content_box.width - tw) / 2.0).max(0.0)
}
strata_style::types::TextAlign::Right => {
let (tw, _) = strata_layout::text::measure_text_bounds_with_family(
&text,
font_size,
line_height,
None,
is_mono,
font_family,
);
(content_box.width - tw).max(0.0)
}
_ => 0.0,
};
let caret_w = 2.0f32;
let caret_h = (font_size * 1.2).min(content_box.height).max(14.0);
let caret_y = content_box.y + ((content_box.height - caret_h) / 2.0).max(0.0);
if let Some((start, end)) = active.state.selection_range() {
let start_clamped = start.min(total_chars);
let end_clamped = end.min(total_chars);
if start_clamped < end_clamped {
let start_prefix: String = text.chars().take(start_clamped).collect();
let (start_w, _) = strata_layout::text::measure_text_bounds_with_family(
&start_prefix,
font_size,
line_height,
None,
is_mono,
font_family,
);
let sel_slice: String = text
.chars()
.skip(start_clamped)
.take(end_clamped - start_clamped)
.collect();
let (sel_w, _) = strata_layout::text::measure_text_bounds_with_family(
&sel_slice,
font_size,
line_height,
None,
is_mono,
font_family,
);
let sel_h = (font_size * 1.25).min(content_box.height);
let sel_y = content_box.y + ((content_box.height - sel_h) / 2.0).max(0.0);
let sel_x = content_box.x + align_offset + start_w;
self.display_list
.push(strata_render::compositor::DrawCommand::DrawRect {
bounds: strata_layout::Rect::new(sel_x, sel_y, sel_w, sel_h),
color: strata_style::types::Color::rgba(59, 130, 246, 120), });
}
}
let has_sel = active.state.has_selection();
let elapsed_ms = self.caret_blink_epoch.elapsed().as_millis();
let is_blink_on = (elapsed_ms / 530).is_multiple_of(2);
if !has_sel && is_blink_on {
let prefix: String = text.chars().take(cursor_idx).collect();
let (cursor_w, _) = strata_layout::text::measure_text_bounds_with_family(
&prefix,
font_size,
line_height,
None,
is_mono,
font_family,
);
let caret_x = (content_box.x + align_offset + cursor_w)
.min(content_box.x + content_box.width - caret_w);
self.display_list
.push(strata_render::compositor::DrawCommand::DrawRect {
bounds: strata_layout::Rect::new(caret_x, caret_y, caret_w, caret_h),
color: caret_color,
});
}
}
pub fn active_caret_rect(&self) -> Option<strata_layout::Rect> {
let active = self.active_focus.as_ref()?;
let mut current_idx = 0;
let target_idx = active.input_index;
let target_id = active.input_id.as_deref();
let is_password = self.input_is_password(target_idx, target_id, active.entity_id);
let layout_by_entity = if let Some(eid) = active.entity_id {
self.layout_root.find_by_entity_id(eid)
} else {
None
};
let layout = layout_by_entity.or_else(|| {
Self::find_input_layout(
&self.root_blueprint,
&self.layout_root,
target_idx,
target_id,
&mut current_idx,
)
})?;
let content_box = layout.geometry.content_box;
if content_box.width <= 0.0 || content_box.height <= 0.0 {
return None;
}
let font_size = layout.style.font_size;
let line_height = layout.style.line_height;
let is_mono = strata_layout::is_monospace_family(layout.style.font_family);
let font_family = layout.style.font_family;
let raw_text = active.state.text.get();
let text = if is_password {
Self::mask_password_text(&raw_text)
} else {
raw_text
};
let total_chars = text.chars().count();
let cursor_idx = active.state.cursor_index.get().min(total_chars);
let align_offset = match layout.style.text_align {
strata_style::types::TextAlign::Center => {
let (tw, _) = strata_layout::text::measure_text_bounds_with_family(
&text,
font_size,
line_height,
None,
is_mono,
font_family,
);
((content_box.width - tw) / 2.0).max(0.0)
}
strata_style::types::TextAlign::Right => {
let (tw, _) = strata_layout::text::measure_text_bounds_with_family(
&text,
font_size,
line_height,
None,
is_mono,
font_family,
);
(content_box.width - tw).max(0.0)
}
_ => 0.0,
};
let caret_w = 2.0f32;
let caret_h = (font_size * 1.2).min(content_box.height).max(14.0);
let caret_y = content_box.y + ((content_box.height - caret_h) / 2.0).max(0.0);
let prefix: String = text.chars().take(cursor_idx).collect();
let (cursor_w, _) = strata_layout::text::measure_text_bounds_with_family(
&prefix,
font_size,
line_height,
None,
is_mono,
font_family,
);
let caret_x = (content_box.x + align_offset + cursor_w)
.min(content_box.x + content_box.width - caret_w);
Some(strata_layout::Rect::new(caret_x, caret_y, caret_w, caret_h))
}
pub fn sync_ime_cursor_location<W: strata_platform::PlatformWindow>(&self, window: &mut W) {
if let Some(rect) = self.active_caret_rect() {
window.set_ime_cursor_location(
rect.x.round() as i32,
rect.y.round() as i32,
rect.width.round() as i32,
rect.height.round() as i32,
);
if let Some(ref active) = self.active_focus {
if self.active_input_is_password(active) {
window.set_ime_surrounding_text("", 0);
} else {
window.set_ime_surrounding_text(
&active.state.text.get(),
active.state.cursor_index.get() as i32,
);
}
}
} else {
window.reset_ime();
}
}
pub fn update_gtk_ime_location(&self) {
#[cfg(target_os = "linux")]
{
if let Some(rect) = self.active_caret_rect() {
strata_platform::gtk_set_ime_cursor_location(
rect.x.round() as i32,
rect.y.round() as i32,
rect.width.round() as i32,
rect.height.round() as i32,
);
if let Some(ref active) = self.active_focus {
if self.active_input_is_password(active) {
strata_platform::gtk_set_ime_surrounding_text("", 0);
} else {
strata_platform::gtk_set_ime_surrounding_text(
&active.state.text.get(),
active.state.cursor_index.get() as i32,
);
}
}
} else {
strata_platform::gtk_reset_ime();
}
}
}
}