SXXXXXXX_PyUCC/pyucc/gui/topbar.py
2025-11-25 12:18:55 +01:00

97 lines
3.5 KiB
Python

import tkinter as tk
from tkinter import ttk, filedialog
from pathlib import Path
from .profile_manager import ProfileManager
from ..config import profiles as profiles_cfg
class TopBar(ttk.Frame):
"""Shared top bar containing profile selection and folder selection.
The TopBar exposes `path_var` (StringVar) and `current_profile` dict
that other tabs can read to apply profile-specific settings.
"""
def __init__(self, parent, *args, **kwargs):
"""Initialize the TopBar.
Args:
parent: The parent Tk widget where the top bar will be placed.
"""
super().__init__(parent, *args, **kwargs)
self.path_var = tk.StringVar()
self.current_profile = None
# Profiles combobox
ttk.Label(self, text="Profile:").grid(row=0, column=0, sticky="w", padx=(8, 4), pady=8)
self.profile_var = tk.StringVar()
self.profile_cb = ttk.Combobox(self, textvariable=self.profile_var, state="readonly")
self._load_profiles()
self.profile_cb.grid(row=0, column=1, sticky="ew", padx=(0, 6))
self.profile_cb.bind("<<ComboboxSelected>>", self._on_profile_selected)
manage_btn = ttk.Button(self, text="Manage...", command=self._open_manager)
manage_btn.grid(row=0, column=2, sticky="e", padx=(0, 4))
# Info area: project type above folder label (read-only, driven by profile)
info = ttk.Frame(self)
info.grid(row=0, column=3, columnspan=3, sticky="ew", padx=(8, 8))
ttk.Label(info, text="Type:").grid(row=0, column=0, sticky="w")
self.project_type_var = tk.StringVar(value="-")
ttk.Label(info, textvariable=self.project_type_var).grid(row=0, column=1, sticky="w", padx=(6, 0))
# Folder display removed: profiles can contain multiple paths now
self.columnconfigure(1, weight=0)
self.columnconfigure(3, weight=1)
def _load_profiles(self):
profs = profiles_cfg.load_profiles()
names = [p.get("name") for p in profs]
self.profile_cb["values"] = names
def _on_profile_selected(self, _evt=None):
name = self.profile_var.get()
if not name:
return
pr = profiles_cfg.find_profile(name)
if not pr:
return
self.current_profile = pr
# Set folder and optionally other UI hints
# prefer new 'paths' list (no legacy compatibility)
paths = pr.get("paths") or []
first = paths[0] if paths else ""
self.path_var.set(first)
# determine a simple project type hint from profile languages
langs = pr.get("languages", []) or []
ptype = ""
if "Python" in langs:
ptype = "Python"
elif "C++" in langs or "C" in langs:
ptype = "C/C++"
elif "Java" in langs:
ptype = "Java"
elif len(langs) == 1:
ptype = langs[0]
elif langs:
ptype = ",".join(langs)
else:
ptype = "Unknown"
self.project_type_var.set(ptype)
def _open_manager(self):
def _refresh():
self._load_profiles()
pm = ProfileManager(self.master, on_change=_refresh)
pm.grab_set()
def browse(self) -> None:
"""Open a directory selection dialog and update `path_var`.
The selected path is stored as an absolute string in `path_var`.
"""
directory = filedialog.askdirectory()
if directory:
self.path_var.set(str(Path(directory)))