-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab_manager.py
More file actions
219 lines (173 loc) · 10.2 KB
/
Copy pathtab_manager.py
File metadata and controls
219 lines (173 loc) · 10.2 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
# tab_manager.py
import tkinter as tk
from tkinter import messagebox
import os
from tkinterdnd2 import DND_FILES
from editor_tab import EditorTab
from event_bus import EventBus
class MockNotebook(tk.Frame):
def __init__(self, parent, tab_mgr, **kwargs):
super().__init__(parent, **kwargs)
self.tab_mgr = tab_mgr
def tabs(self): return tuple(str(id(t)) for t in self.tab_mgr.tabs.keys())
def nametowidget(self, tab_id):
for t in self.tab_mgr.tabs.keys():
if str(id(t)) == str(tab_id): return t
return None
def tab(self, tab_id, option=None, **kwargs):
if option == "text":
tab_widget = self.nametowidget(tab_id)
if tab_widget in self.tab_mgr.tabs:
return self.tab_mgr.tabs[tab_widget]["title"]
return ""
return {}
def select(self, tab_id=None):
if tab_id is None: return str(id(self.tab_mgr.current_tab)) if self.tab_mgr.current_tab else ""
tab_widget = self.nametowidget(tab_id) if isinstance(tab_id, str) else tab_id
if tab_widget: self.tab_mgr.select_tab(tab_widget)
def forget(self, tab_id):
tab_widget = self.nametowidget(tab_id) if isinstance(tab_id, str) else tab_id
if tab_widget: tab_widget.pack_forget()
class TabManager:
def __init__(self, parent_widget):
self.header_frame = tk.Frame(parent_widget, bg="#1E1F1C", bd=0, highlightthickness=0)
self.header_frame.pack(side=tk.TOP, fill=tk.X)
self.btn_left = tk.Button(self.header_frame, text="◄", command=self.scroll_left, bg="#333333", fg="white", relief=tk.FLAT, padx=5, cursor="hand2", takefocus=0)
self.canvas = tk.Canvas(self.header_frame, height=28, bg="#1E1F1C", highlightthickness=0, bd=0, takefocus=0)
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.btn_right = tk.Button(self.header_frame, text="►", command=self.scroll_right, bg="#333333", fg="white", relief=tk.FLAT, padx=5, cursor="hand2", takefocus=0)
self.tab_container = tk.Frame(self.canvas, bg="#1E1F1C", bd=0, highlightthickness=0)
self.canvas_window = self.canvas.create_window((0, 0), window=self.tab_container, anchor="nw")
self.tab_container.bind("<Configure>", self._update_scroll_buttons)
self.canvas.bind("<Configure>", self._update_scroll_buttons)
self.notebook = MockNotebook(parent_widget, self, bd=0, highlightthickness=0, bg="#1E1F1C")
self.notebook.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.notebook.drop_target_register(DND_FILES)
self.notebook.dnd_bind('<<Drop>>', lambda e: EventBus.publish("file:dropped", e))
self.tabs = {}
self.current_tab = None
def _update_scroll_buttons(self, event=None):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
tw = self.tab_container.winfo_reqwidth()
cw = self.canvas.winfo_width()
if tw > cw and cw > 10:
if not self.btn_left.winfo_ismapped():
self.btn_left.pack(side=tk.LEFT, fill=tk.Y, before=self.canvas)
self.btn_right.pack(side=tk.RIGHT, fill=tk.Y, after=self.canvas)
else:
if self.btn_left.winfo_ismapped():
self.btn_left.pack_forget()
self.btn_right.pack_forget()
self.canvas.xview_moveto(0)
def scroll_left(self): self.canvas.xview_scroll(-3, "units")
def scroll_right(self): self.canvas.xview_scroll(3, "units")
def get_current_tab(self): return self.current_tab
def create_editor_tab(self, file_path=None, font_size=12, is_dark=True):
return EditorTab(self.notebook, file_path=file_path, on_change_callback=self.mark_as_modified, font_size=font_size, is_dark=is_dark)
def add_tab(self, tab_frame, title):
btn_frame = tk.Frame(self.tab_container, bg="#555555", cursor="hand2")
btn_frame.pack(side=tk.LEFT, padx=1, pady=(2,0))
display_title = title if len(title) <= 15 else title[:12] + "..."
lbl = tk.Label(btn_frame, text=display_title, width=15, anchor="w", bg="#555555", fg="white", font=("Consolas", 9), cursor="hand2")
lbl.pack(side=tk.LEFT, padx=(5,0), pady=4)
close_btn = tk.Label(btn_frame, text="✖", bg="#555555", fg="#AAAAAA", font=("Consolas", 8), cursor="hand2")
close_btn.pack(side=tk.RIGHT, padx=5)
for w in (btn_frame, lbl):
w.bind("<Button-1>", lambda e, tf=tab_frame: self.select_tab(tf))
w.bind("<Button-2>", lambda e, tf=tab_frame: self.close_tab(tf))
w.bind("<Button-3>", lambda e, tf=tab_frame: self.show_context_menu(e, tf))
close_btn.bind("<Button-1>", lambda e, tf=tab_frame: self.close_tab(tf))
close_btn.bind("<Enter>", lambda e: close_btn.config(fg="#FF5555"))
close_btn.bind("<Leave>", lambda e: close_btn.config(fg="#AAAAAA"))
self.tabs[tab_frame] = {"frame": btn_frame, "lbl": lbl, "title": title}
self.select_tab(tab_frame)
tab_frame.text_area.edit_modified(False)
self._update_scroll_buttons()
def select_tab(self, tab_frame):
if self.current_tab and self.current_tab != tab_frame:
self.current_tab.pack_forget()
self.current_tab = tab_frame
tab_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
for tf, tab_data in self.tabs.items():
bg_color = "#007ACC" if tf == tab_frame else "#555555"
tab_data["frame"].config(bg=bg_color)
tab_data["lbl"].config(bg=bg_color)
tab_data["frame"].winfo_children()[1].config(bg=bg_color)
EventBus.publish("tab:changed")
self.header_frame.after(50, lambda: self._ensure_tab_visible(tab_frame))
def _ensure_tab_visible(self, tab_frame):
if tab_frame not in self.tabs: return
self.canvas.update_idletasks()
btn_frame = self.tabs[tab_frame]["frame"]
btn_x, btn_w = btn_frame.winfo_x(), btn_frame.winfo_width()
canvas_w, scroll_w = self.canvas.winfo_width(), self.tab_container.winfo_width()
if scroll_w <= canvas_w:
self.canvas.xview_moveto(0)
return
view_start_frac, view_end_frac = self.canvas.xview()
visible_x_start, visible_x_end = view_start_frac * scroll_w, view_end_frac * scroll_w
if btn_x < visible_x_start: self.canvas.xview_moveto(btn_x / scroll_w)
elif (btn_x + btn_w) > visible_x_end: self.canvas.xview_moveto((btn_x + btn_w - canvas_w) / scroll_w)
def update_tab_text(self, tab_frame, title):
if tab_frame in self.tabs:
self.tabs[tab_frame]["title"] = title
display_title = title if len(title) <= 15 else title[:12] + "..."
self.tabs[tab_frame]["lbl"].config(text=display_title)
def mark_as_modified(self, tab):
title = os.path.basename(tab.file_path) if tab.file_path else "Adsız"
self.update_tab_text(tab, f"⬤ {title}")
EventBus.publish("tab:content_changed")
def mark_as_saved(self, tab, title):
tab.text_area.edit_modified(False)
self.update_tab_text(tab, title)
EventBus.publish("tab:content_changed")
def show_context_menu(self, event, tab_frame):
menu = tk.Menu(self.header_frame, tearoff=0, font=("Consolas", 10))
menu.add_command(label="❌ Sekmeyi Kapat", command=lambda: self.close_tab(tab_frame))
menu.post(event.x_root, event.y_root)
def close_tab(self, tab_frame):
if tab_frame not in self.tabs: return
if "⬤" in self.tabs[tab_frame]["title"]:
clean_title = os.path.basename(tab_frame.file_path) if tab_frame.file_path else "Adsız"
self.select_tab(tab_frame)
cevap = messagebox.askyesnocancel("Kaydet", f"'{clean_title}' dosyasında değişiklikler var.\nKapatmadan önce kaydetmek ister misiniz?")
if cevap is None: return
elif cevap is True:
success = EventBus.publish("file:save_request", tab_frame)
if not success: return
tab_frame.pack_forget()
self.tabs[tab_frame]["frame"].destroy()
del self.tabs[tab_frame]
tab_frame.destroy()
self._update_scroll_buttons()
remaining_tabs = list(self.tabs.keys())
if remaining_tabs:
self.select_tab(remaining_tabs[-1])
else:
self.current_tab = None
EventBus.publish("tab:empty")
def next_tab(self, event=None):
tabs = list(self.tabs.keys())
if not tabs: return "break"
if self.current_tab in tabs:
idx = tabs.index(self.current_tab)
self.select_tab(tabs[(idx + 1) % len(tabs)])
return "break"
def prev_tab(self, event=None):
tabs = list(self.tabs.keys())
if not tabs: return "break"
if self.current_tab in tabs:
idx = tabs.index(self.current_tab)
self.select_tab(tabs[(idx - 1) % len(tabs)])
return "break"
def apply_theme(self, is_dark):
bg_color, btn_bg, fg_color, active_bg = ("#1E1F1C", "#555555", "white", "#007ACC") if is_dark else ("#F0F0F0", "#CCCCCC", "black", "#007ACC")
self.header_frame.config(bg=bg_color); self.canvas.config(bg=bg_color); self.tab_container.config(bg=bg_color)
self.btn_left.config(bg=btn_bg, fg=fg_color); self.btn_right.config(bg=btn_bg, fg=fg_color); self.notebook.config(bg=bg_color)
for tf, tab_data in self.tabs.items():
current_bg = active_bg if tf == self.current_tab else btn_bg
tab_data["frame"].config(bg=current_bg)
tab_data["lbl"].config(bg=current_bg, fg="white" if tf == self.current_tab else fg_color)
tab_data["frame"].winfo_children()[1].config(bg=current_bg)
for tab_frame in self.tabs.keys():
if isinstance(tab_frame, EditorTab): tab_frame.apply_theme(is_dark)