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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! The question asked before a board is made: what to call it, and the key its
//! cards will carry.
//!
//! Asked rather than assumed. A board is keyed for as long as it exists — the
//! handles already written down go on meaning the cards they meant — so the one
//! moment the key is free is the moment before there are any, and that is this
//! one.
use crate::{model::workspace::Showing, view::root::Cydonia};
use artifact::board::{self, key};
use artifact::space::Member;
use bezel::{
gpui::{
self, AnyElement, App, Context, Entity, Focusable as _, KeyBinding, SharedString,
Subscription, Window, actions, div, prelude::*, px,
},
theme::{TextStyle, Theme, Typeset},
ui::{
input::{FieldEvent, TextField},
widgets::{ButtonStyle, Buttons, Scaffolding as _},
},
};
use std::collections::HashSet;
actions!(cydonia_create, [CommitBoard, DismissBoard]);
/// Claimed on the dialog's fields, so `enter` makes the board.
const CREATE_CONTEXT: &str = "CydoniaNewBoard";
/// How wide the labels run, so the two fields start on one edge. The identity
/// panel's measure — the dialog asks for the same two things.
const LABEL_WIDTH: f32 = 34.;
pub fn bindings() -> Vec<KeyBinding> {
vec![
KeyBinding::new("enter", CommitBoard, Some(CREATE_CONTEXT)),
KeyBinding::new("escape", DismissBoard, Some(CREATE_CONTEXT)),
]
}
/// A board that has been asked for and not yet made. Nothing is on disk until
/// Create: dismissing leaves the project as it was.
pub(crate) struct Making {
/// Which project it lands in. Carried rather than taken from whichever is
/// active, so the dialog makes the board under the heading its `+` was
/// pressed on.
pub project: usize,
/// The pane whose `+` asked for it: the board opens there as a tab.
pub into: Option<Member>,
pub name: Entity<TextField>,
pub key: Entity<TextField>,
/// The key as last derived from the name. While the field still holds it
/// the key follows what is being typed; the moment it is typed over, it is
/// the person's and stays put.
pub derived: SharedString,
/// What was wrong last time. Keeps the dialog open: a taken key is a thing
/// to fix, not to be told about afterwards.
pub error: Option<SharedString>,
/// Holds the name field's subscription for as long as the dialog is up.
_watch: Subscription,
}
/// One of the dialog's fields, holding what it opens with.
fn seed(content: SharedString, placeholder: &'static str, cx: &mut App) -> Entity<TextField> {
let field = cx.new(|cx| {
TextField::new(cx)
.with_key_context(CREATE_CONTEXT)
.with_placeholder(placeholder)
});
field.update(cx, |field, cx| field.set_content(content, cx));
field
}
impl Cydonia {
/// Raise the question. Every New Board comes through here — the sidebar's
/// `+` and the menu bar both — so a board is never made unnamed.
pub(crate) fn ask_new_board(
&mut self,
project: usize,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.ask_new_board_into(project, None, window, cx);
}
/// [`Self::ask_new_board`], opening the board as a tab of `into`.
pub(crate) fn ask_new_board_into(
&mut self,
project: usize,
into: Option<Member>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.commit(cx);
// One editor at a time, as [`Cydonia::open_info`] keeps it.
self.menu = None;
self.renaming = None;
self.info = None;
let derived = self.derived_key(project, "", cx);
let name = seed(SharedString::default(), "name this board…", cx);
let key = seed(derived.clone(), "KEY", cx);
// Subscribed rather than observed: a field notifies on its own caret
// blink, and the key would be rederived twice a second.
let watch = cx.subscribe(&name, |this, field, event: &FieldEvent, cx| {
if *event == FieldEvent::Changed {
let typed = field.read(cx).content().clone();
this.follow_key(&typed, cx);
}
});
window.focus(&name.read(cx).focus_handle(cx), cx);
self.making = Some(Making {
project,
into,
name,
key,
derived,
error: None,
_watch: watch,
});
cx.notify();
}
/// Keep the key following the name, for as long as the key is still the
/// one this put there.
fn follow_key(&mut self, name: &str, cx: &mut Context<Self>) {
let Some((project, field)) = self.making.as_ref().and_then(|making| {
(making.key.read(cx).content() == &making.derived)
.then(|| (making.project, making.key.clone()))
}) else {
return;
};
let derived = self.derived_key(project, name, cx);
field.update(cx, |field, cx| field.set_content(derived.clone(), cx));
if let Some(making) = self.making.as_mut() {
making.derived = derived;
}
}
/// The key a board of this name would take in this project, clear of the
/// keys its neighbours hold. A board still unnamed is keyed off the name it
/// would be filed under, so the field never stands empty or shows the
/// fallback for a name nobody has typed yet.
fn derived_key(&self, project: usize, name: &str, cx: &App) -> SharedString {
let taken: HashSet<String> = self
.workspace
.read(cx)
.projects
.get(project)
.map(|open| open.boards.iter().map(|board| board.key.clone()).collect())
.unwrap_or_default();
let name = match name.trim().is_empty() {
true => board::NAMED,
false => name.trim(),
};
key::derive(name, &taken).into()
}
pub(crate) fn make_board(
&mut self,
_: &CommitBoard,
window: &mut Window,
cx: &mut Context<Self>,
) {
let Some(making) = self.making.as_ref() else {
return;
};
let project = making.project;
let into = making.into.clone();
let key = making.key.read(cx).content().trim().to_owned();
// A board left unnamed is the one the old `+` made outright — see
// [`board::NAMED`]. Nothing else in the sidebar would tell them apart.
let name = match making.name.read(cx).content().trim() {
"" => board::NAMED.to_owned(),
typed => typed.to_owned(),
};
let made = self.workspace.update(cx, |workspace, cx| {
workspace.new_board(project, name, &key, cx)
});
match made {
Ok(ix) => {
self.making = None;
let member = self
.workspace
.read(cx)
.member_of(project, Showing::Board(ix));
match into.zip(member) {
Some((pane, member)) => self.add_tab(&pane, member, window, cx),
None => self.open_board(project, ix, window, cx),
}
}
// Left open, holding what was typed.
Err(why) => {
if let Some(making) = self.making.as_mut() {
making.error = Some(why.into());
}
}
}
cx.notify();
}
pub(crate) fn dismiss_new_board(
&mut self,
_: &DismissBoard,
_: &mut Window,
cx: &mut Context<Self>,
) {
self.making = None;
cx.notify();
}
/// The dialog, over a scrim that takes the press dismissing it — the same
/// shape the delete question is asked in.
pub(crate) fn new_board_dialog(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
let making = self.making.as_ref()?;
let theme = Theme::of(cx).clone();
Some(
div()
.id("new-board-scrim")
.absolute()
.inset_0()
.flex()
.items_center()
.justify_center()
.bg(theme.scrim())
.on_click(cx.listener(|this, _, window, cx| {
this.dismiss_new_board(&DismissBoard, window, cx)
}))
.child(
div()
.id("new-board-dialog")
.w(px(360.))
.flex()
.flex_col()
.gap(px(10.))
.p(px(20.))
.rounded(px(Theme::panel_radius()))
.border_1()
.border_color(theme.border)
.bg(theme.surface)
// The press that answers must not reach the scrim.
.on_click(|_, _, cx| cx.stop_propagation())
.child(theme.row_title("New board"))
.child(self.field_row("Name", making.name.clone(), cx))
.child(self.field_row("Key", making.key.clone(), cx))
// Only on an error: a standing hint would caption two
// labelled fields, and any example would name another
// board.
.children(making.error.clone().map(|why| {
div()
.text_style(TextStyle::Caption)
.text_color(theme.danger)
.child(why)
}))
.child(
div()
.mt(px(4.))
.flex()
.flex_row()
.justify_end()
.gap(px(8.))
.child(
theme
.button("Cancel", ButtonStyle::Ghost, None)
.id("new-board-cancel")
.on_click(cx.listener(|this, _, window, cx| {
this.dismiss_new_board(&DismissBoard, window, cx)
})),
)
.child(
theme
.button("Create", ButtonStyle::Prominent, None)
.id("new-board-create")
.on_click(cx.listener(|this, _, window, cx| {
this.make_board(&CommitBoard, window, cx)
})),
),
),
)
.into_any_element(),
)
}
/// A labelled field, on the identity panel's measure.
fn field_row(&self, label: &str, field: Entity<TextField>, cx: &Context<Self>) -> AnyElement {
let theme = Theme::of(cx).clone();
div()
.flex()
.flex_row()
.items_center()
.gap(px(8.))
.child(
div()
.flex_none()
.w(px(LABEL_WIDTH))
.text_style(TextStyle::Caption)
.text_color(theme.text_muted)
.child(label.to_owned()),
)
.child(div().flex_1().min_w_0().child(field))
.into_any_element()
}
}