chore: prepare for rc version (#34)
**TODOs:** - [x] Fix all clippy issues - [x] Make NIP-4e optional (disabled by default) - [x] Remove support for bunker (Nostr Connect) - [x] Group messages in the same timeframe - [ ] ... Reviewed-on: #34
This commit was merged in pull request #34.
This commit is contained in:
@@ -1,12 +1,4 @@
|
||||
/// Display mapping system for Editor/Input.
|
||||
///
|
||||
/// This module implements a layered display mapping architecture:
|
||||
/// - **WrapMap**: Handles soft-wrapping (buffer → wrap rows)
|
||||
/// - **FoldMap**: Handles folding (wrap rows → display rows)
|
||||
/// - **DisplayMap**: Public facade for Editor/Input
|
||||
///
|
||||
/// The goal is to provide a clean, unified API where Editor only needs to know
|
||||
/// about `BufferPoint ↔ DisplayPoint` mapping, without worrying about internal wrap/fold complexity.
|
||||
#[allow(clippy::module_inception)]
|
||||
mod display_map;
|
||||
mod fold_map;
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use std::ops::Range;
|
||||
use gpui::Half;
|
||||
|
||||
use gpui::{
|
||||
App, Font, LineFragment, Pixels, Point, ShapedLine, Size, TextAlign, Window, point, px,
|
||||
App, Font, Half, LineFragment, Pixels, Point, ShapedLine, Size, TextAlign, Window, point, px,
|
||||
size,
|
||||
};
|
||||
use ropey::Rope;
|
||||
@@ -97,7 +96,7 @@ impl TextWrapper {
|
||||
/// Get the line item by row index.
|
||||
#[inline]
|
||||
pub(crate) fn line(&self, row: usize) -> Option<&LineItem> {
|
||||
self.lines.iter().skip(row).next()
|
||||
self.lines.get(row)
|
||||
}
|
||||
|
||||
pub(crate) fn set_wrap_width(&mut self, wrap_width: Option<Pixels>, cx: &mut App) {
|
||||
@@ -228,7 +227,7 @@ impl TextWrapper {
|
||||
});
|
||||
}
|
||||
|
||||
if self.lines.len() == 0 {
|
||||
if self.lines.is_empty() {
|
||||
self.lines = new_lines;
|
||||
} else {
|
||||
self.lines.splice(rows_range, new_lines);
|
||||
@@ -246,7 +245,7 @@ impl TextWrapper {
|
||||
///
|
||||
/// If the `text` is the same as the current text, do nothing.
|
||||
fn update_all(&mut self, text: &Rope, cx: &mut App) {
|
||||
self.update(text, &(0..text.len()), &text, cx);
|
||||
self.update(text, &(0..text.len()), text, cx);
|
||||
}
|
||||
|
||||
/// Return display point (with soft wrap) from the given byte offset in the text.
|
||||
@@ -278,7 +277,8 @@ impl TextWrapper {
|
||||
// Otherwise return the eof of the line.
|
||||
let last_range = line.wrapped_lines.last().unwrap_or(&(0..0));
|
||||
let ix = line.lines_len().saturating_sub(1);
|
||||
return WrapDisplayPoint::new(wrapped_row + ix, ix, last_range.len());
|
||||
|
||||
WrapDisplayPoint::new(wrapped_row + ix, ix, last_range.len())
|
||||
}
|
||||
|
||||
/// Return byte offset in the text from the given display point (with soft wrap).
|
||||
@@ -301,7 +301,7 @@ impl TextWrapper {
|
||||
wrapped_row += line.lines_len();
|
||||
}
|
||||
|
||||
return self.text.len();
|
||||
self.text.len()
|
||||
}
|
||||
|
||||
pub(crate) fn display_point_to_point(&self, point: WrapDisplayPoint) -> TreeSitterPoint {
|
||||
@@ -580,351 +580,3 @@ impl LineLayout {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::rc::Rc;
|
||||
|
||||
use gpui::{Boundary, FontFeatures, FontStyle, FontWeight, px};
|
||||
|
||||
#[test]
|
||||
fn test_update() {
|
||||
let font = gpui::Font {
|
||||
family: "Arial".into(),
|
||||
weight: FontWeight::default(),
|
||||
style: FontStyle::Normal,
|
||||
features: FontFeatures::default(),
|
||||
fallbacks: None,
|
||||
};
|
||||
|
||||
let mut wrapper = TextWrapper::new(font, px(14.), None);
|
||||
let mut text = Rope::from(
|
||||
"Hello, 世界!\r\nThis is second line.\nThis is third line.\n这里是第 4 行。",
|
||||
);
|
||||
|
||||
fn fake_wrap_line(_line: &str, _wrap_width: Pixels) -> Vec<Boundary> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn assert_wrapper_lines(text: &Rope, wrapper: &TextWrapper, expected_lines: &[&[&str]]) {
|
||||
let mut actual_lines = vec![];
|
||||
let mut offset = 0;
|
||||
for line in wrapper.lines.iter() {
|
||||
actual_lines.push(
|
||||
line.wrapped_lines
|
||||
.iter()
|
||||
.map(|range| text.slice(offset + range.start..offset + range.end))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
// +1 \n
|
||||
offset += line.len() + 1;
|
||||
}
|
||||
assert_eq!(actual_lines, expected_lines);
|
||||
}
|
||||
|
||||
wrapper._update(&text, &(0..text.len()), &text, &mut fake_wrap_line);
|
||||
assert_eq!(wrapper.lines.len(), 4);
|
||||
assert_wrapper_lines(
|
||||
&text,
|
||||
&wrapper,
|
||||
&[
|
||||
&["Hello, 世界!\r"],
|
||||
&["This is second line."],
|
||||
&["This is third line."],
|
||||
&["这里是第 4 行。"],
|
||||
],
|
||||
);
|
||||
|
||||
// Add a new text to end
|
||||
let range = text.len()..text.len();
|
||||
let new_text = "New text";
|
||||
text.replace(range.clone(), new_text);
|
||||
wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
|
||||
assert_eq!(
|
||||
text.to_string(),
|
||||
"Hello, 世界!\r\nThis is second line.\nThis is third line.\n这里是第 4 行。New text"
|
||||
);
|
||||
assert_eq!(wrapper.lines.len(), 4);
|
||||
assert_eq!(wrapper.lines.len(), 4);
|
||||
assert_wrapper_lines(
|
||||
&text,
|
||||
&wrapper,
|
||||
&[
|
||||
&["Hello, 世界!\r"],
|
||||
&["This is second line."],
|
||||
&["This is third line."],
|
||||
&["这里是第 4 行。New text"],
|
||||
],
|
||||
);
|
||||
|
||||
// Replace first line `Hello` to `AAA`
|
||||
let range = 0..5;
|
||||
let new_text = "AAA";
|
||||
text.replace(range.clone(), new_text);
|
||||
wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
|
||||
assert_eq!(
|
||||
text.to_string(),
|
||||
"AAA, 世界!\r\nThis is second line.\nThis is third line.\n这里是第 4 行。New text"
|
||||
);
|
||||
assert_eq!(wrapper.lines.len(), 4);
|
||||
assert_wrapper_lines(
|
||||
&text,
|
||||
&wrapper,
|
||||
&[
|
||||
&["AAA, 世界!\r"],
|
||||
&["This is second line."],
|
||||
&["This is third line."],
|
||||
&["这里是第 4 行。New text"],
|
||||
],
|
||||
);
|
||||
|
||||
// Remove the second line
|
||||
let start_offset = text.line_start_offset(1);
|
||||
let end_offset = text.line_end_offset(1);
|
||||
let range = start_offset..end_offset + 1;
|
||||
text.replace(range.clone(), "");
|
||||
wrapper._update(&text, &range, &Rope::from(""), &mut fake_wrap_line);
|
||||
assert_eq!(
|
||||
text.to_string(),
|
||||
"AAA, 世界!\r\nThis is third line.\n这里是第 4 行。New text"
|
||||
);
|
||||
assert_eq!(wrapper.lines.len(), 3);
|
||||
assert_wrapper_lines(
|
||||
&text,
|
||||
&wrapper,
|
||||
&[
|
||||
&["AAA, 世界!\r"],
|
||||
&["This is third line."],
|
||||
&["这里是第 4 行。New text"],
|
||||
],
|
||||
);
|
||||
|
||||
// Replace the first 2 lines to "This is a new line."
|
||||
let range = text.line_start_offset(0)..text.line_end_offset(1) + 1;
|
||||
let new_text = "This is a new line.\nThis is new line 2.\n";
|
||||
text.replace(range.clone(), new_text);
|
||||
wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
|
||||
assert_eq!(
|
||||
text.to_string(),
|
||||
"This is a new line.\nThis is new line 2.\n这里是第 4 行。New text"
|
||||
);
|
||||
assert_eq!(wrapper.lines.len(), 3);
|
||||
assert_wrapper_lines(
|
||||
&text,
|
||||
&wrapper,
|
||||
&[
|
||||
&["This is a new line."],
|
||||
&["This is new line 2."],
|
||||
&["这里是第 4 行。New text"],
|
||||
],
|
||||
);
|
||||
|
||||
// Add a new line at the end
|
||||
let range = text.len()..text.len();
|
||||
let new_text = "\nThis is a new line at the end.";
|
||||
text.replace(range.clone(), new_text);
|
||||
wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
|
||||
assert_eq!(
|
||||
text.to_string(),
|
||||
"This is a new line.\nThis is new line 2.\n这里是第 4 行。New text\nThis is a new line at the end."
|
||||
);
|
||||
assert_eq!(wrapper.lines.len(), 4);
|
||||
assert_wrapper_lines(
|
||||
&text,
|
||||
&wrapper,
|
||||
&[
|
||||
&["This is a new line."],
|
||||
&["This is new line 2."],
|
||||
&["这里是第 4 行。New text"],
|
||||
&["This is a new line at the end."],
|
||||
],
|
||||
);
|
||||
|
||||
// Add a new line at the beginning
|
||||
let range = 0..0;
|
||||
let new_text = "This is a new line at the beginning.\n";
|
||||
text.replace(range.clone(), new_text);
|
||||
wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
|
||||
assert_eq!(
|
||||
text.to_string(),
|
||||
"This is a new line at the beginning.\nThis is a new line.\nThis is new line 2.\n这里是第 4 行。New text\nThis is a new line at the end."
|
||||
);
|
||||
assert_eq!(wrapper.lines.len(), 5);
|
||||
assert_wrapper_lines(
|
||||
&text,
|
||||
&wrapper,
|
||||
&[
|
||||
&["This is a new line at the beginning."],
|
||||
&["This is a new line."],
|
||||
&["This is new line 2."],
|
||||
&["这里是第 4 行。New text"],
|
||||
&["This is a new line at the end."],
|
||||
],
|
||||
);
|
||||
|
||||
// Remove all to at least one line in `lines`.
|
||||
let range = 0..text.len();
|
||||
let new_text = "";
|
||||
text.replace(range.clone(), new_text);
|
||||
wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
|
||||
assert_eq!(text.to_string(), "");
|
||||
assert_eq!(wrapper.lines.len(), 1);
|
||||
assert_eq!(wrapper.lines[0].wrapped_lines, vec![0..0]);
|
||||
|
||||
// Test update_all
|
||||
let range = 0..text.len();
|
||||
let new_text = "This is a full text.\nThis is a second line.";
|
||||
text.replace(range.clone(), new_text);
|
||||
wrapper._update(&text, &range, &text, &mut fake_wrap_line);
|
||||
assert_eq!(
|
||||
text.to_string(),
|
||||
"This is a full text.\nThis is a second line."
|
||||
);
|
||||
assert_eq!(wrapper.lines.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_line_layout() {
|
||||
let mut line_layout = LineLayout::new();
|
||||
|
||||
let line1 = ShapedLine::default().with_len(100);
|
||||
let line2 = ShapedLine::default().with_len(50);
|
||||
let wrapped_lines = smallvec::smallvec![line1, line2];
|
||||
line_layout.set_wrapped_lines(wrapped_lines);
|
||||
assert_eq!(line_layout.len(), 150);
|
||||
assert_eq!(line_layout.wrapped_lines.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_for_index_prefers_first_leading_empty_visual_line() {
|
||||
let mut line_layout = LineLayout::new();
|
||||
line_layout.set_wrapped_lines(smallvec::smallvec![
|
||||
ShapedLine::default(),
|
||||
ShapedLine::default(),
|
||||
ShapedLine::default().with_len(3),
|
||||
]);
|
||||
|
||||
let last_layout = LastLayout {
|
||||
visible_range: 0..1,
|
||||
visible_buffer_lines: vec![0],
|
||||
visible_line_byte_offsets: vec![0],
|
||||
visible_top: px(0.),
|
||||
visible_range_offset: 0..0,
|
||||
lines: Rc::new(vec![]),
|
||||
line_height: px(20.),
|
||||
wrap_width: None,
|
||||
line_number_width: px(0.),
|
||||
cursor_bounds: None,
|
||||
text_align: TextAlign::Left,
|
||||
content_width: px(0.),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
line_layout.position_for_index(0, &last_layout, false),
|
||||
Some(point(px(0.), px(0.)))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_offset_to_display_point() {
|
||||
let font = gpui::Font {
|
||||
family: "Arial".into(),
|
||||
weight: FontWeight::default(),
|
||||
style: FontStyle::Normal,
|
||||
features: FontFeatures::default(),
|
||||
fallbacks: None,
|
||||
};
|
||||
|
||||
let mut wrapper = TextWrapper::new(font, px(14.), None);
|
||||
wrapper.text = Rope::from(
|
||||
"Hello, 世界!\r\nThis is second line.\nThis is third line.\n这里是第 4 行。",
|
||||
);
|
||||
wrapper.lines = vec![
|
||||
// range: 0..15
|
||||
LineItem {
|
||||
line: Rope::from("Hello, 世界!\r"),
|
||||
wrapped_lines: vec![0..15],
|
||||
},
|
||||
// range: 16..36
|
||||
LineItem {
|
||||
line: Rope::from("This is second line."),
|
||||
wrapped_lines: vec![0..10, 10..20],
|
||||
},
|
||||
// range: 37..56
|
||||
LineItem {
|
||||
line: Rope::from("This is third line."),
|
||||
wrapped_lines: vec![0..9, 9..15, 15..20],
|
||||
},
|
||||
// range: 57..79
|
||||
LineItem {
|
||||
line: Rope::from("这里是第 4 行。"),
|
||||
wrapped_lines: vec![0..22],
|
||||
},
|
||||
];
|
||||
|
||||
assert_eq!(
|
||||
wrapper.offset_to_display_point(12),
|
||||
WrapDisplayPoint::new(0, 0, 12)
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.offset_to_display_point(15),
|
||||
WrapDisplayPoint::new(0, 0, 15)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
wrapper.offset_to_display_point(16),
|
||||
WrapDisplayPoint::new(1, 0, 0)
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.offset_to_display_point(21),
|
||||
WrapDisplayPoint::new(1, 0, 5)
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.offset_to_display_point(27),
|
||||
WrapDisplayPoint::new(2, 1, 1)
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.offset_to_display_point(37),
|
||||
WrapDisplayPoint::new(3, 0, 0)
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.offset_to_display_point(54),
|
||||
WrapDisplayPoint::new(5, 2, 2)
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.offset_to_display_point(59),
|
||||
WrapDisplayPoint::new(6, 0, 2)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
wrapper.display_point_to_offset(WrapDisplayPoint::new(6, 0, 2)),
|
||||
59
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.display_point_to_offset(WrapDisplayPoint::new(5, 2, 2)),
|
||||
54
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.display_point_to_offset(WrapDisplayPoint::new(3, 0, 0)),
|
||||
37
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.display_point_to_offset(WrapDisplayPoint::new(2, 1, 1)),
|
||||
27
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.display_point_to_offset(WrapDisplayPoint::new(1, 0, 5)),
|
||||
21
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.display_point_to_offset(WrapDisplayPoint::new(1, 0, 0)),
|
||||
16
|
||||
);
|
||||
assert_eq!(
|
||||
wrapper.display_point_to_offset(WrapDisplayPoint::new(0, 0, 15)),
|
||||
15
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,11 +131,14 @@ impl Element for EditorScrollbar {
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> (LayoutId, Self::RequestLayoutState) {
|
||||
let mut style = Style::default();
|
||||
style.position = Position::Absolute;
|
||||
style.size.width = relative(1.).into();
|
||||
style.size.height = relative(1.).into();
|
||||
|
||||
let style = Style {
|
||||
position: Position::Absolute,
|
||||
size: Size {
|
||||
width: relative(1.).into(),
|
||||
height: relative(1.).into(),
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
(window.request_layout(style, [], cx), ())
|
||||
}
|
||||
|
||||
@@ -309,13 +312,12 @@ impl TextElement {
|
||||
let mut cursor_bounds = None;
|
||||
|
||||
// If the input has a fixed height (Otherwise is auto-grow), we need to add a bottom margin to the input.
|
||||
let top_bottom_margin = if state.mode.is_auto_grow() {
|
||||
line_height
|
||||
} else if visible_range.len() < BOTTOM_MARGIN_ROWS * 8 {
|
||||
line_height
|
||||
} else {
|
||||
BOTTOM_MARGIN_ROWS * line_height
|
||||
};
|
||||
let top_bottom_margin =
|
||||
if state.mode.is_auto_grow() || visible_range.len() < BOTTOM_MARGIN_ROWS * 8 {
|
||||
line_height
|
||||
} else {
|
||||
BOTTOM_MARGIN_ROWS * line_height
|
||||
};
|
||||
|
||||
// The cursor corresponds to the current cursor position in the text no only the line.
|
||||
let mut cursor_pos = None;
|
||||
|
||||
@@ -18,7 +18,7 @@ pub(crate) fn input_style(disabled: bool, cx: &App) -> (Hsla, Hsla) {
|
||||
if disabled {
|
||||
(cx.theme().surface_background, cx.theme().text_muted)
|
||||
} else {
|
||||
(cx.theme().surface_background, cx.theme().text)
|
||||
(cx.theme().elevated_surface_background, cx.theme().text)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ pub use index_path::IndexPath;
|
||||
pub use kbd::*;
|
||||
pub use root::{Root, window_paddings};
|
||||
pub use styled::*;
|
||||
pub use title_bar::*;
|
||||
pub use window_ext::*;
|
||||
|
||||
pub use crate::Disableable;
|
||||
@@ -41,6 +42,7 @@ mod index_path;
|
||||
mod kbd;
|
||||
mod root;
|
||||
mod styled;
|
||||
mod title_bar;
|
||||
mod window_ext;
|
||||
|
||||
/// Initialize the UI module.
|
||||
|
||||
@@ -298,10 +298,10 @@ impl Render for Notification {
|
||||
|
||||
let action = self.action_builder.clone().map(|builder| {
|
||||
builder(self, window, cx)
|
||||
.xsmall()
|
||||
.small()
|
||||
.primary()
|
||||
.px_3()
|
||||
.font_semibold()
|
||||
.px_4()
|
||||
.font_medium()
|
||||
});
|
||||
|
||||
let icon = match self.kind {
|
||||
@@ -364,8 +364,14 @@ impl Render for Notification {
|
||||
})
|
||||
.when_some(content, |this, content| this.child(content))
|
||||
.when_some(action, |this, action| {
|
||||
this.gap_2()
|
||||
.child(h_flex().w_full().flex_1().justify_end().child(action))
|
||||
this.gap_2().child(
|
||||
h_flex()
|
||||
.mt_2()
|
||||
.w_full()
|
||||
.flex_1()
|
||||
.justify_end()
|
||||
.child(action),
|
||||
)
|
||||
}),
|
||||
)
|
||||
.child(
|
||||
|
||||
352
crates/ui/src/title_bar.rs
Normal file
352
crates/ui/src/title_bar.rs
Normal file
@@ -0,0 +1,352 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
AnyElement, App, ClickEvent, Context, Decorations, Hsla, InteractiveElement, IntoElement,
|
||||
MouseButton, ParentElement, Pixels, Render, RenderOnce, StatefulInteractiveElement as _,
|
||||
StyleRefinement, Styled, TitlebarOptions, Window, WindowControlArea, div, px,
|
||||
};
|
||||
use smallvec::SmallVec;
|
||||
use theme::ActiveTheme;
|
||||
|
||||
use crate::{Icon, IconName, InteractiveElementExt as _, Sizable as _, StyledExt, h_flex};
|
||||
|
||||
pub const TITLE_BAR_HEIGHT: Pixels = px(34.);
|
||||
#[cfg(target_os = "macos")]
|
||||
pub const TRAFFIC_LIGHT_PADDING: f32 = 80.;
|
||||
|
||||
/// TitleBar used to customize the appearance of the title bar.
|
||||
///
|
||||
/// We can put some elements inside the title bar.
|
||||
#[derive(IntoElement)]
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub struct TitleBar {
|
||||
style: StyleRefinement,
|
||||
children: SmallVec<[AnyElement; 1]>,
|
||||
on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>,
|
||||
}
|
||||
|
||||
impl TitleBar {
|
||||
/// Create a new TitleBar.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
style: StyleRefinement::default(),
|
||||
children: SmallVec::new(),
|
||||
on_close_window: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the default title bar options for compatible with the [`crate::TitleBar`].
|
||||
pub fn title_bar_options() -> TitlebarOptions {
|
||||
TitlebarOptions {
|
||||
title: None,
|
||||
appears_transparent: true,
|
||||
traffic_light_position: Some(gpui::point(px(9.0), px(9.0))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Add custom for close window event, default is None, then click X button will call `window.remove_window()`.
|
||||
/// Linux only, this will do nothing on other platforms.
|
||||
pub fn on_close_window(
|
||||
mut self,
|
||||
f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
|
||||
) -> Self {
|
||||
if cfg!(target_os = "linux") {
|
||||
self.on_close_window = Some(Rc::new(Box::new(f)));
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TitleBar {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
// The Windows control buttons have a fixed width of 35px.
|
||||
//
|
||||
// We don't need implementation the click event for the control buttons.
|
||||
// If user clicked in the bounds, the window event will be triggered.
|
||||
#[derive(IntoElement, Clone)]
|
||||
#[allow(clippy::type_complexity)]
|
||||
enum ControlIcon {
|
||||
Minimize,
|
||||
Restore,
|
||||
Maximize,
|
||||
Close {
|
||||
on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>,
|
||||
},
|
||||
}
|
||||
|
||||
impl ControlIcon {
|
||||
fn minimize() -> Self {
|
||||
Self::Minimize
|
||||
}
|
||||
|
||||
fn restore() -> Self {
|
||||
Self::Restore
|
||||
}
|
||||
|
||||
fn maximize() -> Self {
|
||||
Self::Maximize
|
||||
}
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
fn close(on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>) -> Self {
|
||||
Self::Close { on_close_window }
|
||||
}
|
||||
|
||||
fn id(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Minimize => "minimize",
|
||||
Self::Restore => "restore",
|
||||
Self::Maximize => "maximize",
|
||||
Self::Close { .. } => "close",
|
||||
}
|
||||
}
|
||||
|
||||
fn icon(&self) -> IconName {
|
||||
match self {
|
||||
Self::Minimize => IconName::WindowMinimize,
|
||||
Self::Restore => IconName::WindowRestore,
|
||||
Self::Maximize => IconName::WindowMaximize,
|
||||
Self::Close { .. } => IconName::WindowClose,
|
||||
}
|
||||
}
|
||||
|
||||
fn window_control_area(&self) -> WindowControlArea {
|
||||
match self {
|
||||
Self::Minimize => WindowControlArea::Min,
|
||||
Self::Restore | Self::Maximize => WindowControlArea::Max,
|
||||
Self::Close { .. } => WindowControlArea::Close,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_close(&self) -> bool {
|
||||
matches!(self, Self::Close { .. })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn hover_fg(&self, cx: &App) -> Hsla {
|
||||
if self.is_close() {
|
||||
cx.theme().danger_foreground
|
||||
} else {
|
||||
cx.theme().text
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn hover_bg(&self, cx: &App) -> Hsla {
|
||||
if self.is_close() {
|
||||
cx.theme().danger_background
|
||||
} else {
|
||||
cx.theme().ghost_element_hover
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn active_bg(&self, cx: &mut App) -> Hsla {
|
||||
if self.is_close() {
|
||||
cx.theme().danger_active
|
||||
} else {
|
||||
cx.theme().ghost_element_active
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for ControlIcon {
|
||||
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let is_linux = cfg!(target_os = "linux");
|
||||
let is_windows = cfg!(target_os = "windows");
|
||||
|
||||
let icon = self.clone();
|
||||
let hover_fg = self.hover_fg(cx);
|
||||
let hover_bg = self.hover_bg(cx);
|
||||
let active_bg = self.active_bg(cx);
|
||||
|
||||
let on_close_window = match &self {
|
||||
ControlIcon::Close { on_close_window } => on_close_window.clone(),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
div()
|
||||
.id(self.id())
|
||||
.flex()
|
||||
.w(TITLE_BAR_HEIGHT)
|
||||
.h_full()
|
||||
.flex_shrink_0()
|
||||
.justify_center()
|
||||
.content_center()
|
||||
.items_center()
|
||||
.text_color(cx.theme().text)
|
||||
.hover(|style| style.bg(hover_bg).text_color(hover_fg))
|
||||
.active(|style| style.bg(active_bg).text_color(hover_fg))
|
||||
.when(is_windows, |this| {
|
||||
this.window_control_area(self.window_control_area())
|
||||
})
|
||||
.when(is_linux, |this| {
|
||||
this.on_mouse_down(MouseButton::Left, move |_, window, cx| {
|
||||
window.prevent_default();
|
||||
cx.stop_propagation();
|
||||
})
|
||||
.on_click(move |_, window, cx| {
|
||||
cx.stop_propagation();
|
||||
match icon {
|
||||
Self::Minimize => window.minimize_window(),
|
||||
Self::Restore | Self::Maximize => window.zoom_window(),
|
||||
Self::Close { .. } => {
|
||||
if let Some(f) = on_close_window.clone() {
|
||||
f(&ClickEvent::default(), window, cx);
|
||||
} else {
|
||||
window.remove_window();
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
.child(Icon::new(self.icon()).small())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(IntoElement)]
|
||||
#[allow(clippy::type_complexity)]
|
||||
struct WindowControls {
|
||||
on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>,
|
||||
}
|
||||
|
||||
impl RenderOnce for WindowControls {
|
||||
fn render(self, window: &mut Window, _: &mut App) -> impl IntoElement {
|
||||
if cfg!(target_os = "macos") || cfg!(target_family = "wasm") {
|
||||
return div().id("window-controls");
|
||||
}
|
||||
|
||||
h_flex()
|
||||
.id("window-controls")
|
||||
.items_center()
|
||||
.flex_shrink_0()
|
||||
.h_full()
|
||||
.child(ControlIcon::minimize())
|
||||
.child(if window.is_maximized() {
|
||||
ControlIcon::restore()
|
||||
} else {
|
||||
ControlIcon::maximize()
|
||||
})
|
||||
.child(ControlIcon::close(self.on_close_window))
|
||||
}
|
||||
}
|
||||
|
||||
impl Styled for TitleBar {
|
||||
fn style(&mut self) -> &mut gpui::StyleRefinement {
|
||||
&mut self.style
|
||||
}
|
||||
}
|
||||
|
||||
impl ParentElement for TitleBar {
|
||||
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
|
||||
self.children.extend(elements);
|
||||
}
|
||||
}
|
||||
|
||||
struct TitleBarState {
|
||||
should_move: bool,
|
||||
}
|
||||
|
||||
impl Render for TitleBarState {
|
||||
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
|
||||
div()
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for TitleBar {
|
||||
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let is_client_decorated = matches!(window.window_decorations(), Decorations::Client { .. });
|
||||
let is_web = cfg!(target_family = "wasm");
|
||||
let is_linux = cfg!(target_os = "linux");
|
||||
let is_macos = cfg!(target_os = "macos");
|
||||
|
||||
let state = window.use_state(cx, |_, _| TitleBarState { should_move: false });
|
||||
|
||||
div().flex_shrink_0().child(
|
||||
div()
|
||||
.id("title-bar")
|
||||
.flex()
|
||||
.flex_row()
|
||||
.items_center()
|
||||
.justify_between()
|
||||
.h(TITLE_BAR_HEIGHT)
|
||||
.map(|this| {
|
||||
if window.is_fullscreen() {
|
||||
this.px_2()
|
||||
} else if cx.theme().platform.is_mac() {
|
||||
this.pr_2().pl(px(TRAFFIC_LIGHT_PADDING))
|
||||
} else {
|
||||
this.px_2()
|
||||
}
|
||||
})
|
||||
.border_b_1()
|
||||
.border_color(cx.theme().border)
|
||||
.bg(cx.theme().title_bar)
|
||||
.refine_style(&self.style)
|
||||
.when(is_linux, |this| {
|
||||
this.on_double_click(|_, window, _| window.zoom_window())
|
||||
})
|
||||
.when(is_macos, |this| {
|
||||
this.on_double_click(|_, window, _| window.titlebar_double_click())
|
||||
})
|
||||
.on_mouse_down_out(window.listener_for(&state, |state, _, _, _| {
|
||||
state.should_move = false;
|
||||
}))
|
||||
.on_mouse_down(
|
||||
MouseButton::Left,
|
||||
window.listener_for(&state, |state, _, _, _| {
|
||||
state.should_move = true;
|
||||
}),
|
||||
)
|
||||
.on_mouse_up(
|
||||
MouseButton::Left,
|
||||
window.listener_for(&state, |state, _, _, _| {
|
||||
state.should_move = false;
|
||||
}),
|
||||
)
|
||||
.on_mouse_move(window.listener_for(&state, |state, _, window, _| {
|
||||
if state.should_move {
|
||||
state.should_move = false;
|
||||
window.start_window_move();
|
||||
}
|
||||
}))
|
||||
.child(
|
||||
h_flex()
|
||||
.id("bar")
|
||||
.h_full()
|
||||
.justify_between()
|
||||
.flex_shrink_0()
|
||||
.flex_1()
|
||||
.when(!is_web, |this| {
|
||||
this.window_control_area(WindowControlArea::Drag)
|
||||
.when(window.is_fullscreen(), |this| this.pl_3())
|
||||
.when(is_linux && is_client_decorated, |this| {
|
||||
this.child(
|
||||
div()
|
||||
.top_0()
|
||||
.left_0()
|
||||
.absolute()
|
||||
.size_full()
|
||||
.h_full()
|
||||
.on_mouse_down(
|
||||
MouseButton::Right,
|
||||
move |ev, window, _| {
|
||||
window.show_window_menu(ev.position)
|
||||
},
|
||||
),
|
||||
)
|
||||
})
|
||||
})
|
||||
.children(self.children),
|
||||
)
|
||||
.child(WindowControls {
|
||||
on_close_window: self.on_close_window,
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user