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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use crate::app::App;
use ratatui::Frame;
use ratatui::layout::{Alignment, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, List, ListItem};
/// 命令定义
struct CommandItem {
key: &'static str,
label: &'static str,
color: Option<Color>,
}
/// 渲染空格命令菜单(小型弹窗样式)
pub fn render(f: &mut Frame, area: Rect, app: &App) {
// 渲染半透明背景遮罩
render_backdrop(f, area);
// 创建小型居中弹窗(固定宽度40,高度自适应内容)
let popup_area = centered_rect_fixed(40, 20, area);
// 清空弹窗区域
f.render_widget(Clear, popup_area);
use crate::app::MenuState;
// 根据菜单状态显示不同的命令列表
let (commands, title) = match app.menu_state {
Some(MenuState::Project) => {
// 检查当前项目是否是当前目录的本地项目
let is_current_local_project = if let Some(project) = app.get_focused_project() {
if project.project_type == crate::models::ProjectType::Local {
let current_local_dir = crate::fs::get_local_kanban_dir();
project.path.starts_with(¤t_local_dir)
} else {
false
}
} else {
false
};
let mut commands = vec![
CommandItem {
key: "o",
label: "打开项目",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
CommandItem {
key: "n",
label: "新建本地项目 [L]",
color: None,
},
CommandItem {
key: "N",
label: "新建全局项目 [G]",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
];
// 根据项目类型决定显示哪些删除选项
if is_current_local_project {
// 当前目录的本地项目:只能硬删除
commands.push(CommandItem {
key: "D",
label: "删除项目文件",
color: None,
});
} else {
// 全局项目或其他目录的本地项目:可以软删除或硬删除
commands.push(CommandItem {
key: "d",
label: "隐藏项目(软删除)",
color: None,
});
commands.push(CommandItem {
key: "D",
label: "删除项目文件",
color: None,
});
}
commands.push(CommandItem {
key: "",
label: "",
color: None,
});
commands.push(CommandItem {
key: "r",
label: "重命名项目",
color: None,
});
commands.push(CommandItem {
key: "i",
label: "复制项目信息",
color: None,
});
(commands, " 项目操作 ")
}
Some(MenuState::Window) => (
vec![
CommandItem {
key: "w",
label: "下一个窗口",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
CommandItem {
key: "v",
label: "垂直分屏",
color: None,
},
CommandItem {
key: "s",
label: "水平分屏",
color: None,
},
CommandItem {
key: "q",
label: "关闭当前窗口",
color: None,
},
CommandItem {
key: "m",
label: "最大化/恢复",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
CommandItem {
key: "h",
label: "聚焦左侧",
color: None,
},
CommandItem {
key: "l",
label: "聚焦右侧",
color: None,
},
CommandItem {
key: "k",
label: "聚焦上方",
color: None,
},
CommandItem {
key: "j",
label: "聚焦下方",
color: None,
},
],
" 窗口操作 ",
),
Some(MenuState::Task) => (
vec![
CommandItem {
key: "a",
label: "新建任务",
color: None,
},
CommandItem {
key: "e",
label: "编辑任务",
color: None,
},
CommandItem {
key: "E",
label: "用编辑器编辑",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
CommandItem {
key: "v",
label: "预览任务",
color: None,
},
CommandItem {
key: "V",
label: "外部预览",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
CommandItem {
key: "t",
label: "编辑标签",
color: None,
},
CommandItem {
key: "Y",
label: "复制到剪贴板",
color: None,
},
CommandItem {
key: "d",
label: "删除任务",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
CommandItem {
key: "h",
label: "优先级:高",
color: Some(Color::Red),
},
CommandItem {
key: "m",
label: "优先级:中",
color: Some(Color::Yellow),
},
CommandItem {
key: "l",
label: "优先级:低",
color: Some(Color::Green),
},
CommandItem {
key: "n",
label: "优先级:无",
color: None,
},
],
" 任务操作 ",
),
Some(MenuState::Status) => (
vec![
CommandItem {
key: "a",
label: "创建新状态",
color: None,
},
CommandItem {
key: "r",
label: "重命名状态",
color: None,
},
CommandItem {
key: "e",
label: "编辑显示名",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
CommandItem {
key: "h",
label: "左移状态列",
color: None,
},
CommandItem {
key: "l",
label: "右移状态列",
color: None,
},
CommandItem {
key: "H",
label: "移到最左侧",
color: None,
},
CommandItem {
key: "L",
label: "移到最右侧",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
CommandItem {
key: "d",
label: "删除状态",
color: None,
},
],
" 状态管理 ",
),
Some(MenuState::Main) | None => (
vec![
CommandItem {
key: "f",
label: "快速切换项目",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
CommandItem {
key: "p",
label: "项目操作...",
color: None,
},
CommandItem {
key: "w",
label: "窗口操作...",
color: None,
},
CommandItem {
key: "t",
label: "任务操作...",
color: None,
},
CommandItem {
key: "s",
label: "状态管理...",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
CommandItem {
key: "r",
label: "重新加载当前项目",
color: None,
},
CommandItem {
key: "R",
label: "重新加载所有项目",
color: None,
},
CommandItem {
key: "",
label: "",
color: None,
},
CommandItem {
key: "?",
label: "显示帮助",
color: None,
},
CommandItem {
key: "q",
label: "退出",
color: None,
},
],
" 命令菜单 ",
),
};
// 获取有效命令索引映射(排除空行)
let mut valid_cmd_indices: Vec<usize> = Vec::new();
for (i, cmd) in commands.iter().enumerate() {
if !cmd.key.is_empty() {
valid_cmd_indices.push(i);
}
}
// 构建列表项
let selected_cmd_idx = app
.menu_selected_index
.and_then(|sel_idx| valid_cmd_indices.get(sel_idx).copied());
let list_items: Vec<ListItem> = commands
.iter()
.enumerate()
.map(|(idx, cmd)| {
let is_selected = Some(idx) == selected_cmd_idx;
if cmd.key.is_empty() {
// 空行分隔符
ListItem::new("")
} else {
let mut spans = vec![
Span::raw(" "),
Span::styled(
format!("{:3}", cmd.key),
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::raw(" "),
];
// 如果有颜色,添加彩色圆点
if let Some(color) = cmd.color {
spans.push(Span::styled("● ", Style::default().fg(color)));
}
spans.push(Span::styled(cmd.label, Style::default().fg(Color::White)));
let line = Line::from(spans);
let mut item = ListItem::new(line);
// 为选中项添加高亮背景
if is_selected {
item = item.style(Style::default().bg(Color::Rgb(76, 86, 106))); // Nord highlight
}
item
}
})
.collect();
let list = List::new(list_items).block(
Block::default()
.title(title)
.title_style(
Style::default()
.fg(Color::Rgb(235, 203, 139)) // Nord yellow
.add_modifier(Modifier::BOLD),
)
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Rgb(136, 192, 208))) // Nord cyan
.border_type(ratatui::widgets::BorderType::Rounded)
.style(Style::default().bg(Color::Rgb(46, 52, 64))), // Nord background
);
f.render_widget(list, popup_area);
// 底部提示
let help_area = Rect {
x: popup_area.x,
y: popup_area.y + popup_area.height,
width: popup_area.width,
height: 1,
};
if help_area.y < area.height {
let help_line = Line::from(vec![
Span::styled(" ", Style::default()),
Span::styled("↑↓", Style::default().fg(Color::Cyan)),
Span::styled(" 导航 ", Style::default().fg(Color::DarkGray)),
Span::styled("Esc", Style::default().fg(Color::Cyan)),
Span::styled(" 返回 ", Style::default().fg(Color::DarkGray)),
Span::styled("Enter", Style::default().fg(Color::Cyan)),
Span::styled(" 执行", Style::default().fg(Color::DarkGray)),
]);
f.render_widget(
ratatui::widgets::Paragraph::new(help_line)
.alignment(Alignment::Center)
.style(Style::default().bg(Color::Rgb(0, 0, 0))),
help_area,
);
}
}
/// 创建固定大小的居中矩形
fn centered_rect_fixed(width: u16, height: u16, r: Rect) -> Rect {
let x = r.x + (r.width.saturating_sub(width)) / 2;
let y = r.y + (r.height.saturating_sub(height)) / 2;
Rect {
x,
y,
width: width.min(r.width),
height: height.min(r.height),
}
}
/// 渲染半透明背景遮罩
fn render_backdrop(f: &mut Frame, area: Rect) {
let block = Block::default().style(Style::default().bg(Color::Rgb(0, 0, 0)));
f.render_widget(block, area);
}