Repository navigation
Expand file tree
/
Copy pathwindow_view.py
More file actions
591 lines (505 loc) · 19.4 KB
/
Copy pathwindow_view.py
File metadata and controls
591 lines (505 loc) · 19.4 KB
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
"""GTK widget-tree construction for the chat window.
Builds and returns widget handles only. Controllers, Gio actions, and
behavioral callback wiring remain on ChatSidebar.
"""
from __future__ import annotations
from dataclasses import dataclass
import gi
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
gi.require_version("Gdk", "4.0")
from gi.repository import Adw, Gdk, Gio, Gtk, Pango
def _use_pointer_cursor(widget: Gtk.Widget) -> None:
"""Show the pointer/hand cursor while hovering a clickable widget."""
try:
widget.set_cursor_from_name("pointer")
except Exception: # noqa: BLE001
pass
def setup_model_name_item(_factory: Gtk.SignalListItemFactory, item: Gtk.ListItem) -> None:
"""Create the truncated label used for a model dropdown row."""
label = Gtk.Label()
label.set_xalign(0)
label.set_ellipsize(Pango.EllipsizeMode.MIDDLE)
label.set_max_width_chars(36)
item.set_child(label)
def bind_model_name_item(_factory: Gtk.SignalListItemFactory, item: Gtk.ListItem) -> None:
"""Fill a model dropdown row; the full name stays on the tooltip."""
label = item.get_child()
obj = item.get_item()
text = obj.get_string() if isinstance(obj, Gtk.StringObject) else ""
if isinstance(label, Gtk.Label):
label.set_text(text or "")
label.set_tooltip_text(text or "")
def _model_name_label_factory() -> Gtk.SignalListItemFactory:
"""List-item factory showing model names truncated with an ellipsis.
Long registry-style names (e.g. ``hf.co/...:Q4_K_M``) must not force
the sidebar wider — the label keeps at most 36 characters and the
full name remains available as a tooltip.
"""
factory = Gtk.SignalListItemFactory()
factory.connect("setup", setup_model_name_item)
factory.connect("bind", bind_model_name_item)
return factory
@dataclass(frozen=True)
class HistorySidebarWidgets:
root: Gtk.Box
history_list: Gtk.ListBox
model_combo: Gtk.DropDown
new_btn: Gtk.Button
settings_btn: Gtk.Button
@dataclass(frozen=True)
class HeaderWidgets:
header: Adw.HeaderBar
chat_title_label: Gtk.Label
sidebar_btn: Gtk.ToggleButton
clear_btn: Gtk.Button
refresh_btn: Gtk.Button
menu_btn: Gtk.MenuButton
@dataclass(frozen=True)
class HealthBannerWidgets:
banner: Gtk.Widget
title: Gtk.Label
detail: Gtk.Label
action_btn: Gtk.Button
@dataclass(frozen=True)
class ComposerWidgets:
input: Gtk.TextView
input_scroll: Gtk.ScrolledWindow
placeholder: Gtk.Label
hint: Gtk.Label
char_label: Gtk.Label
send_btn: Gtk.Button
stop_btn: Gtk.Button
bar: Gtk.Box
@dataclass(frozen=True)
class LoadOverlayWidgets:
overlay: Gtk.Box
title: Gtk.Label
model_label: Gtk.Label
status: Gtk.Label
progress: Gtk.ProgressBar
spinner: Gtk.Spinner
@dataclass(frozen=True)
class ChatChromeWidgets:
root: Gtk.Box
toolbar_view: Adw.ToolbarView
sidebar: HistorySidebarWidgets
header: HeaderWidgets
health: HealthBannerWidgets
def build_history_sidebar(*, width: int) -> HistorySidebarWidgets:
"""Docked left rail: Chats header, model selection, recent list, settings footer."""
side = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
side.add_css_class("chat-sidebar")
side.set_hexpand(False)
side.set_vexpand(True)
side.set_size_request(width, -1)
head = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=4)
head.add_css_class("chat-sidebar-header")
title = Gtk.Label(label="Chats")
title.add_css_class("chat-sidebar-title")
title.set_halign(Gtk.Align.START)
title.set_hexpand(True)
title.set_xalign(0)
head.append(title)
new_btn = Gtk.Button.new_from_icon_name("document-new-symbolic")
new_btn.add_css_class("flat")
new_btn.set_tooltip_text("New conversation")
new_btn.set_size_request(32, 32)
_use_pointer_cursor(new_btn)
head.append(new_btn)
side.append(head)
# Model selection sits above Recent, with a rule separating the two blocks.
model_block = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
model_block.add_css_class("chat-sidebar-model-block")
model_block.set_hexpand(True)
model_section = Gtk.Label(label="Model Selection")
model_section.add_css_class("chat-sidebar-section")
model_section.set_halign(Gtk.Align.START)
model_section.set_xalign(0)
model_block.append(model_section)
model_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
model_box.add_css_class("chat-sidebar-model")
model_box.set_hexpand(True)
model_combo = Gtk.DropDown.new_from_strings(["Loading models…"])
model_combo.set_hexpand(True)
model_combo.set_halign(Gtk.Align.FILL)
model_combo.set_valign(Gtk.Align.CENTER)
model_combo.set_size_request(-1, 38)
model_combo.set_factory(_model_name_label_factory())
model_combo.set_list_factory(_model_name_label_factory())
_use_pointer_cursor(model_combo)
model_box.append(model_combo)
model_block.append(model_box)
side.append(model_block)
section = Gtk.Label(label="Recent")
section.add_css_class("chat-sidebar-section")
section.set_halign(Gtk.Align.START)
section.set_xalign(0)
side.append(section)
history_list = Gtk.ListBox()
history_list.add_css_class("navigation-sidebar")
history_list.set_selection_mode(Gtk.SelectionMode.SINGLE)
history_list.set_activate_on_single_click(True)
scroll = Gtk.ScrolledWindow()
scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
scroll.set_vexpand(True)
scroll.set_hexpand(True)
scroll.set_child(history_list)
side.append(scroll)
foot = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
foot.add_css_class("chat-sidebar-footer")
settings_btn = Gtk.Button.new_from_icon_name("emblem-system-symbolic")
try:
display = Gdk.Display.get_default()
theme = Gtk.IconTheme.get_for_display(display) if display else None
if theme is not None and not theme.has_icon("emblem-system-symbolic"):
if theme.has_icon("preferences-system-symbolic"):
settings_btn.set_icon_name("preferences-system-symbolic")
except Exception: # noqa: BLE001
settings_btn.set_icon_name("preferences-system-symbolic")
settings_btn.add_css_class("flat")
settings_btn.set_tooltip_text("Settings")
settings_btn.set_size_request(32, 32)
settings_btn.set_halign(Gtk.Align.START)
_use_pointer_cursor(settings_btn)
foot.append(settings_btn)
side.append(foot)
return HistorySidebarWidgets(
root=side,
history_list=history_list,
model_combo=model_combo,
new_btn=new_btn,
settings_btn=settings_btn,
)
def build_header() -> HeaderWidgets:
"""Standard window header chrome (no CSD title buttons)."""
header = Adw.HeaderBar()
header.set_show_end_title_buttons(False)
header.set_show_start_title_buttons(False)
title_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
title_box.set_valign(Gtk.Align.CENTER)
title = Gtk.Label(label="ChickenButt")
title.add_css_class("header-title")
title.set_halign(Gtk.Align.CENTER)
chat_title_label = Gtk.Label(label="New conversation")
chat_title_label.add_css_class("header-sub")
chat_title_label.set_halign(Gtk.Align.CENTER)
chat_title_label.set_ellipsize(Pango.EllipsizeMode.END)
chat_title_label.set_max_width_chars(36)
title_box.append(title)
title_box.append(chat_title_label)
header.set_title_widget(title_box)
sidebar_btn = Gtk.ToggleButton()
sidebar_btn.set_icon_name("sidebar-show-symbolic")
sidebar_btn.set_tooltip_text("Show or hide chat list")
sidebar_btn.set_valign(Gtk.Align.CENTER)
sidebar_btn.set_active(False)
_use_pointer_cursor(sidebar_btn)
header.pack_start(sidebar_btn)
clear_btn = Gtk.Button.new_from_icon_name("edit-clear-all-symbolic")
clear_btn.set_tooltip_text("Clear conversation")
clear_btn.set_valign(Gtk.Align.CENTER)
_use_pointer_cursor(clear_btn)
header.pack_start(clear_btn)
menu = Gio.Menu()
menu.append("New Conversation", "win.new-chat")
menu.append("Show Chat List", "win.toggle-sidebar")
menu.append("Settings", "win.settings")
menu.append("Export Chat Markdown…", "win.export-current-md")
menu.append("Export Chat JSON…", "win.export-current-json")
win_section = Gio.Menu()
win_section.append("Hide", "win.hide")
win_section.append("Maximize", "win.maximize")
win_section.append("Close", "win.close")
menu.append_section(None, win_section)
menu.append("Quit", "app.quit")
menu_btn = Gtk.MenuButton()
menu_btn.set_icon_name("open-menu-symbolic")
menu_btn.set_menu_model(menu)
menu_btn.set_valign(Gtk.Align.CENTER)
menu_btn.set_tooltip_text("Menu")
_use_pointer_cursor(menu_btn)
# pack_end: first widget sits at the far right edge
header.pack_end(menu_btn)
refresh_btn = Gtk.Button.new_from_icon_name("view-refresh-symbolic")
refresh_btn.set_tooltip_text("Refresh models (Ctrl+R)")
refresh_btn.add_css_class("flat")
refresh_btn.set_valign(Gtk.Align.CENTER)
refresh_btn.set_action_name("win.refresh-models")
_use_pointer_cursor(refresh_btn)
# Immediately left of the burger menu (pack_end: menu first = rightmost)
header.pack_end(refresh_btn)
return HeaderWidgets(
header=header,
chat_title_label=chat_title_label,
sidebar_btn=sidebar_btn,
clear_btn=clear_btn,
refresh_btn=refresh_btn,
menu_btn=menu_btn,
)
def build_health_banner() -> HealthBannerWidgets:
"""Ollama health / onboarding banner above the transcript."""
health_clamp = Adw.Clamp()
health_clamp.set_maximum_size(768)
health_clamp.set_tightening_threshold(400)
health_clamp.set_hexpand(True)
health_clamp.set_margin_top(10)
health_clamp.set_margin_bottom(6)
health_clamp.set_margin_start(24)
health_clamp.set_margin_end(24)
health_inner = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
health_inner.add_css_class("health-banner")
health_text = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
health_text.set_hexpand(True)
title = Gtk.Label(label="")
title.add_css_class("health-banner-title")
title.set_halign(Gtk.Align.START)
title.set_wrap(True)
title.set_xalign(0)
detail = Gtk.Label(label="")
detail.add_css_class("health-banner-detail")
detail.set_halign(Gtk.Align.START)
detail.set_wrap(True)
detail.set_xalign(0)
health_text.append(title)
health_text.append(detail)
health_inner.append(health_text)
action_btn = Gtk.Button(label="Retry")
action_btn.add_css_class("suggested-action")
action_btn.set_valign(Gtk.Align.CENTER)
_use_pointer_cursor(action_btn)
health_inner.append(action_btn)
health_clamp.set_child(health_inner)
health_clamp.set_visible(False)
return HealthBannerWidgets(
banner=health_clamp,
title=title,
detail=detail,
action_btn=action_btn,
)
def build_composer(*, char_limit: int) -> ComposerWidgets:
"""Messaging-style composer shell (widgets only; no geometry controller)."""
composer_inner = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
composer_inner.set_hexpand(True)
shell = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
shell.add_css_class("composer-shell")
shell.set_hexpand(True)
input_view = Gtk.TextView()
input_view.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
input_view.set_accepts_tab(False)
input_view.set_top_margin(8)
input_view.set_bottom_margin(8)
input_view.set_left_margin(2)
input_view.set_right_margin(4)
input_view.set_pixels_above_lines(1)
input_view.set_pixels_below_lines(1)
input_view.set_hexpand(True)
input_view.set_vexpand(False)
input_view.add_css_class("composer-input")
input_scroll = Gtk.ScrolledWindow()
input_scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.NEVER)
input_scroll.set_propagate_natural_height(True)
input_scroll.set_propagate_natural_width(True)
input_scroll.set_hexpand(True)
input_scroll.set_vexpand(False)
input_scroll.set_valign(Gtk.Align.CENTER)
input_scroll.add_css_class("composer-scroll")
input_scroll.set_child(input_view)
placeholder = Gtk.Label(label="Message…")
placeholder.add_css_class("dim-label")
placeholder.set_halign(Gtk.Align.START)
placeholder.set_valign(Gtk.Align.CENTER)
placeholder.set_margin_start(4)
placeholder.set_can_target(False)
input_overlay = Gtk.Overlay()
input_overlay.set_child(input_scroll)
input_overlay.add_overlay(placeholder)
input_overlay.set_hexpand(True)
input_overlay.set_vexpand(False)
input_overlay.set_valign(Gtk.Align.CENTER)
stop_btn = Gtk.Button.new_from_icon_name("media-playback-stop-symbolic")
stop_btn.add_css_class("circular")
stop_btn.add_css_class("stop-btn")
stop_btn.add_css_class("destructive-action")
stop_btn.set_tooltip_text("Stop generating")
stop_btn.set_valign(Gtk.Align.CENTER)
stop_btn.set_visible(False)
_use_pointer_cursor(stop_btn)
send_icon = "mail-send-symbolic"
try:
display = Gdk.Display.get_default()
theme = Gtk.IconTheme.get_for_display(display) if display else None
if theme is not None:
for candidate in (
"paper-plane-symbolic",
"mail-send-symbolic",
"document-send-symbolic",
"go-up-symbolic",
):
if theme.has_icon(candidate):
send_icon = candidate
break
except Exception: # noqa: BLE001
pass
send_btn = Gtk.Button.new_from_icon_name(send_icon)
send_btn.add_css_class("circular")
send_btn.add_css_class("send-btn")
send_btn.add_css_class("suggested-action")
send_btn.set_tooltip_text("Send message (Enter)")
send_btn.set_valign(Gtk.Align.CENTER)
_use_pointer_cursor(send_btn)
shell.append(input_overlay)
shell.append(stop_btn)
shell.append(send_btn)
hint = Gtk.Label(
label="Enter to send · Shift+Enter for newline · Esc to minimize to tray"
)
hint.add_css_class("composer-hint")
hint.set_halign(Gtk.Align.CENTER)
hint.set_hexpand(True)
hint.set_justify(Gtk.Justification.CENTER)
hint.set_wrap(True)
hint.set_xalign(0.5)
char_label = Gtk.Label(label="")
char_label.add_css_class("composer-char-count")
char_label.set_halign(Gtk.Align.END)
char_label.set_hexpand(True)
char_label.set_visible(False)
char_label.set_tooltip_text(f"Hard safety limit is {char_limit:,} characters")
meta_row = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
meta_row.add_css_class("composer-meta-row")
meta_row.set_hexpand(True)
meta_row.append(char_label)
composer_inner.append(hint)
composer_inner.append(shell)
composer_inner.append(meta_row)
composer_clamp = Adw.Clamp()
composer_clamp.set_maximum_size(768)
composer_clamp.set_tightening_threshold(400)
composer_clamp.set_hexpand(True)
composer_clamp.set_child(composer_inner)
composer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
composer.add_css_class("composer-bar")
composer.set_vexpand(False)
composer.set_hexpand(True)
composer.set_valign(Gtk.Align.END)
composer.set_margin_start(24)
composer.set_margin_end(24)
composer.append(composer_clamp)
return ComposerWidgets(
input=input_view,
input_scroll=input_scroll,
placeholder=placeholder,
hint=hint,
char_label=char_label,
send_btn=send_btn,
stop_btn=stop_btn,
bar=composer,
)
def build_load_overlay() -> LoadOverlayWidgets:
"""Model warm-up cover widgets (initially hidden)."""
veil = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
veil.add_css_class("load-overlay")
veil.set_hexpand(True)
veil.set_vexpand(True)
veil.set_halign(Gtk.Align.FILL)
veil.set_valign(Gtk.Align.FILL)
veil.set_can_target(True)
center = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
center.set_halign(Gtk.Align.CENTER)
center.set_valign(Gtk.Align.CENTER)
center.set_hexpand(True)
center.set_vexpand(True)
card = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
card.add_css_class("load-card")
card.set_halign(Gtk.Align.CENTER)
spinner = Gtk.Spinner()
spinner.set_size_request(36, 36)
spinner.set_halign(Gtk.Align.CENTER)
spinner.start()
card.append(spinner)
title = Gtk.Label(label="Loading model")
title.add_css_class("load-title")
title.set_halign(Gtk.Align.CENTER)
title.set_margin_top(16)
card.append(title)
model_label = Gtk.Label(label="")
model_label.add_css_class("load-model")
model_label.set_halign(Gtk.Align.CENTER)
model_label.set_ellipsize(Pango.EllipsizeMode.MIDDLE)
model_label.set_max_width_chars(36)
card.append(model_label)
progress = Gtk.ProgressBar()
progress.add_css_class("load-progress")
progress.set_show_text(False)
progress.set_fraction(0.0)
progress.pulse()
card.append(progress)
status = Gtk.Label(label="Connecting to Ollama…")
status.add_css_class("load-status")
status.set_halign(Gtk.Align.CENTER)
status.set_wrap(True)
status.set_justify(Gtk.Justification.CENTER)
status.set_max_width_chars(40)
card.append(status)
center.append(card)
veil.append(center)
veil.set_visible(False)
return LoadOverlayWidgets(
overlay=veil,
title=title,
model_label=model_label,
status=status,
progress=progress,
spinner=spinner,
)
def build_chat_chrome(*, sidebar_width: int) -> ChatChromeWidgets:
"""Root horizontal layout: history rail + toolbar/header chrome + health banner."""
root = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
root.set_hexpand(True)
root.set_vexpand(True)
sidebar = build_history_sidebar(width=sidebar_width)
root.append(sidebar.root)
sidebar.root.set_visible(False)
toolbar_view = Adw.ToolbarView()
toolbar_view.set_hexpand(True)
toolbar_view.set_vexpand(True)
root.append(toolbar_view)
header = build_header()
toolbar_view.add_top_bar(header.header)
health = build_health_banner()
return ChatChromeWidgets(
root=root,
toolbar_view=toolbar_view,
sidebar=sidebar,
header=header,
health=health,
)
def assemble_chat_surface(
*,
toolbar_view: Adw.ToolbarView,
health_banner: Gtk.Widget,
transcript_widget: Gtk.Widget,
composer_bar: Gtk.Widget,
load_overlay: Gtk.Widget,
) -> Gtk.Overlay:
"""Compose transcript + composer under the health banner with a load overlay."""
chat_column = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
chat_column.add_css_class("chat-surface")
chat_column.set_hexpand(True)
chat_column.set_vexpand(True)
chat_column.append(transcript_widget)
chat_column.append(composer_bar)
outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
outer.set_hexpand(True)
outer.set_vexpand(True)
health_banner.set_vexpand(False)
outer.append(health_banner)
outer.append(chat_column)
root_overlay = Gtk.Overlay()
root_overlay.set_hexpand(True)
root_overlay.set_vexpand(True)
root_overlay.set_child(outer)
root_overlay.add_overlay(load_overlay)
# Caller sets toolbar content (chat vs settings stack lives on ChatSidebar).
return root_overlay