# gui.py import tkinter as tk from tkinter import ttk from tkinter import scrolledtext from tkinter import filedialog from tkinter import messagebox from tkinter import simpledialog import logging import os import re # Ensure re is imported # Import constant from the central location from config_manager import DEFAULT_BACKUP_DIR # --- Tooltip Class Definition --- class Tooltip: """Simple tooltip implementation for Tkinter widgets.""" def __init__(self, widget, text): """Initialize tooltip.""" self.widget = widget self.text = text self.tooltip_window = None self.id = None self.x = 0 self.y = 0 def showtip(self): """Display text in a tooltip window.""" self.hidetip() # Hide any existing tooltip first if not self.widget.winfo_exists(): return # Avoid error if widget destroyed try: # Get widget position relative to widget itself x_rel, y_rel, _, _ = self.widget.bbox("insert") # Get widget position relative to screen x_root = self.widget.winfo_rootx() y_root = self.widget.winfo_rooty() # Calculate final screen position with offset x_pos = x_root + x_rel + 25 y_pos = y_root + y_rel + 25 except tk.TclError: # Fallback position calculation if bbox fails x_root = self.widget.winfo_rootx() y_root = self.widget.winfo_rooty() widget_width = self.widget.winfo_width() widget_height = self.widget.winfo_height() x_pos = x_root + widget_width // 2 y_pos = y_root + widget_height + 5 # Create the tooltip window as a Toplevel self.tooltip_window = tk.Toplevel(self.widget) tw = self.tooltip_window # Remove window decorations (border, title bar) tw.wm_overrideredirect(True) # Position the window (ensure integer coordinates) tw.wm_geometry(f"+{int(x_pos)}+{int(y_pos)}") # Create the label inside the tooltip window label = tk.Label( tw, text=self.text, justify=tk.LEFT, background="#ffffe0", # Light yellow background relief=tk.SOLID, borderwidth=1, font=("tahoma", "8", "normal") ) label.pack(ipadx=1) def hidetip(self): """Hide the tooltip window.""" tw = self.tooltip_window self.tooltip_window = None if tw: try: # Check if window still exists before destroying if tw.winfo_exists(): tw.destroy() except tk.TclError: # Handle cases where window might already be destroyed pass # --- End Tooltip Class --- # --- Gitignore Editor Window Class --- class GitignoreEditorWindow(tk.Toplevel): """ Toplevel window for editing the .gitignore file. """ def __init__(self, master, gitignore_path, logger): """ Initialize the editor window. """ super().__init__(master) self.gitignore_path = gitignore_path self.logger = logger # Store original content to check for changes on close self.original_content = "" # Window Configuration self.title(f"Edit {os.path.basename(gitignore_path)}") self.geometry("600x450") self.minsize(400, 300) self.grab_set() # Make window modal self.transient(master) # Keep window on top of parent self.protocol("WM_DELETE_WINDOW", self._on_close) # Handle close button # Main frame with padding main_frame = ttk.Frame(self, padding="10") main_frame.pack(fill=tk.BOTH, expand=True) # Configure grid weights for resizing text area main_frame.rowconfigure(0, weight=1) main_frame.columnconfigure(0, weight=1) # ScrolledText widget for editing content self.text_editor = scrolledtext.ScrolledText( main_frame, wrap=tk.WORD, font=("Consolas", 10), # Monospaced font undo=True # Enable undo/redo functionality ) self.text_editor.grid(row=0, column=0, sticky="nsew", pady=(0, 10)) # Frame for buttons at the bottom button_frame = ttk.Frame(main_frame) button_frame.grid(row=1, column=0, sticky="ew") # Configure button frame columns to center buttons button_frame.columnconfigure(0, weight=1) # Spacer left button_frame.columnconfigure(3, weight=1) # Spacer right # Save button self.save_button = ttk.Button( button_frame, text="Save and Close", command=self._save_and_close ) self.save_button.grid(row=0, column=2, padx=5) # Right-center # Cancel button self.cancel_button = ttk.Button( button_frame, text="Cancel", command=self._on_close ) self.cancel_button.grid(row=0, column=1, padx=5) # Left-center # Load initial file content self._load_file() # Center window relative to parent self._center_window(master) # Set initial focus to the text editor self.text_editor.focus_set() def _center_window(self, parent): """Centers the editor window relative to its parent.""" self.update_idletasks() # Ensure window size is calculated correctly # Get parent window geometry parent_x = parent.winfo_rootx() parent_y = parent.winfo_rooty() parent_width = parent.winfo_width() parent_height = parent.winfo_height() # Get self (editor window) geometry win_width = self.winfo_width() win_height = self.winfo_height() # Calculate position for centering x_pos = parent_x + (parent_width // 2) - (win_width // 2) y_pos = parent_y + (parent_height // 2) - (win_height // 2) # Prevent window going off-screen (basic check) screen_width = self.winfo_screenwidth() screen_height = self.winfo_screenheight() x_pos = max(0, min(x_pos, screen_width - win_width)) y_pos = max(0, min(y_pos, screen_height - win_height)) # Apply the calculated position self.geometry(f"+{int(x_pos)}+{int(y_pos)}") def _load_file(self): """Loads the content of the .gitignore file into the editor.""" self.logger.info(f"Loading content for: {self.gitignore_path}") try: content = "" # Default empty content if os.path.exists(self.gitignore_path): # Read file content with specified encoding and error handling with open(self.gitignore_path, 'r', encoding='utf-8', errors='replace') as f: content = f.read() self.logger.debug(".gitignore content loaded.") else: # File doesn't exist self.logger.info(f"'{self.gitignore_path}' does not exist.") # Store original content and update editor self.original_content = content self.text_editor.delete("1.0", tk.END) # Clear previous content self.text_editor.insert(tk.END, self.original_content) # Reset undo stack after loading new content self.text_editor.edit_reset() except IOError as e: # Handle file reading errors self.logger.error(f"Read error: {e}", exc_info=True) messagebox.showerror( "Error Reading File", f"Could not read the .gitignore file:\n{e}", parent=self ) except Exception as e: # Handle other unexpected errors during loading self.logger.exception(f"Unexpected load error: {e}") messagebox.showerror( "Unexpected Error", f"An unexpected error occurred loading file:\n{e}", parent=self ) def _save_file(self): """Saves the current editor content to the .gitignore file.""" # Get content, normalize whitespace and newline current_content = self.text_editor.get("1.0", tk.END).rstrip() if current_content: current_content += "\n" # Normalize original content similarly for comparison normalized_original = self.original_content.rstrip() if normalized_original: normalized_original += "\n" # Check if content actually changed if current_content == normalized_original: self.logger.info("No changes detected in .gitignore. Skipping save.") return True # Indicate success (no action needed) # Proceed with saving if content changed self.logger.info(f"Saving changes to: {self.gitignore_path}") try: # Write content to file with UTF-8 encoding and consistent newline with open(self.gitignore_path, 'w', encoding='utf-8', newline='\n') as f: f.write(current_content) self.logger.info(".gitignore file saved successfully.") # Update original content state and reset undo stack self.original_content = current_content self.text_editor.edit_reset() return True # Indicate save success except IOError as e: # Handle file writing errors self.logger.error(f"Write error: {e}", exc_info=True) messagebox.showerror("Error Saving File", f"Could not save the .gitignore file:\n{e}", parent=self) return False # Indicate save failure except Exception as e: # Handle other unexpected errors during saving self.logger.exception(f"Unexpected save error: {e}") messagebox.showerror("Unexpected Error", f"An unexpected error occurred saving file:\n{e}", parent=self) return False def _save_and_close(self): """Saves the file and closes the window if save is successful.""" save_successful = self._save_file() if save_successful: # Close window only if save succeeded or no changes were made self.destroy() def _on_close(self): """Handles closing the window (Cancel button or WM close button).""" # Get current content and normalize it current_content = self.text_editor.get("1.0", tk.END).rstrip() if current_content: current_content += "\n" # Normalize original content normalized_original = self.original_content.rstrip() if normalized_original: normalized_original += "\n" # Check if content has changed since loading/last save if current_content != normalized_original: # Ask user about saving changes (Yes/No/Cancel) response = messagebox.askyesnocancel( "Unsaved Changes", "You have unsaved changes.\nSave before closing?", parent=self ) if response is True: # User chose Yes (Save) self._save_and_close() # Attempts save, closes only if successful elif response is False: # User chose No (Discard) self.logger.warning("Discarding unsaved changes in editor.") self.destroy() # Close immediately # Else (response is None - Cancel): Do nothing, keep window open else: # No changes detected, simply close the window self.destroy() # --- End Gitignore Editor Window --- # --- Create Tag Dialog --- class CreateTagDialog(simpledialog.Dialog): """ Dialog to get new tag name and message. """ def __init__(self, parent, title="Create New Tag"): """Initialize the dialog.""" self.tag_name_var = tk.StringVar() self.tag_message_var = tk.StringVar() self.result = None # Stores (name, message) tuple on success # Call Dialog constructor AFTER initializing variables super().__init__(parent, title=title) def body(self, master): """Create dialog body with input fields.""" name_label = ttk.Label(master, text="Tag Name:") name_label.grid(row=0, column=0, padx=5, pady=5, sticky="w") self.name_entry = ttk.Entry( master, textvariable=self.tag_name_var, width=40 ) self.name_entry.grid(row=0, column=1, padx=5, pady=5, sticky="ew") message_label = ttk.Label(master, text="Tag Message:") message_label.grid(row=1, column=0, padx=5, pady=5, sticky="w") self.message_entry = ttk.Entry( master, textvariable=self.tag_message_var, width=40 ) self.message_entry.grid(row=1, column=1, padx=5, pady=5, sticky="ew") # Configure column to allow entry widgets to expand horizontally master.columnconfigure(1, weight=1) # Return the widget that should have initial focus return self.name_entry def validate(self): """Validate the input fields are not empty and name format is valid.""" name = self.tag_name_var.get().strip() message = self.tag_message_var.get().strip() # Check for empty fields if not name: messagebox.showwarning("Input Error", "Tag name cannot be empty.", parent=self) return 0 # Fail validation if not message: messagebox.showwarning("Input Error", "Tag message cannot be empty.", parent=self) return 0 # Fail validation # Validate tag name format using regex (ensure 're' is imported) # Pattern based on git check-ref-format rules pattern = r"^(?![./]|.*([./]{2,}|[.]$|\.lock$|@\{))[^ \t\n\r\f\v~^:?*[\\]+(?") # For branch display # --- Create Main Layout Sections --- # Profile selection is always visible at the top self._create_profile_frame() # --- Create Notebook for Tabs --- # Add padding below tabs for separation from action buttons self.notebook = ttk.Notebook(self, padding=(0, 5, 0, 0)) self.notebook.pack(pady=5, padx=0, fill="both", expand=True) # --- Create Frames for each Tab --- # Add padding within each tab frame for content spacing self.setup_tab_frame = ttk.Frame(self.notebook, padding=(10)) self.commit_branch_tab_frame = ttk.Frame(self.notebook, padding=(10)) self.tags_gitignore_tab_frame = ttk.Frame(self.notebook, padding=(10)) # Add frames as tabs to the notebook with descriptive text self.notebook.add(self.setup_tab_frame, text=' Setup & Backup ') # Combined setup self.notebook.add(self.commit_branch_tab_frame, text=' Commit & Branches ') self.notebook.add(self.tags_gitignore_tab_frame, text=' Tags & Gitignore ') # --- Populate Tabs with Widgets --- self._populate_setup_tab() self._populate_commit_branch_tab() self._populate_tags_gitignore_tab() # --- Core Actions Frame (Below Tabs) --- self._create_function_frame() # --- Log Area (Bottom) --- self._create_log_area() # --- Initial State Configuration --- self._initialize_profile_selection() # Set initial state of backup widgets based on checkbox value self.toggle_backup_dir() def _create_profile_frame(self): """Creates the frame for profile selection and management.""" self.profile_frame = ttk.LabelFrame( self, text="Profile Configuration", padding=(10, 5) ) # Pack frame at the top, below potential menu bar, expand horizontally self.profile_frame.pack(pady=(0, 5), fill="x") # Allow dropdown column (column 1) to expand horizontally self.profile_frame.columnconfigure(1, weight=1) # Profile Label profile_label = ttk.Label(self.profile_frame, text="Profile:") profile_label.grid(row=0, column=0, sticky=tk.W, padx=5, pady=5) # Profile Dropdown (Combobox) self.profile_dropdown = ttk.Combobox( self.profile_frame, textvariable=self.profile_var, state="readonly", # Prevent typing custom values width=35, values=self.initial_profile_sections # Set initial list ) self.profile_dropdown.grid(row=0, column=1, sticky=tk.EW, padx=5, pady=5) # Bind selection change to load profile settings self.profile_dropdown.bind( "<>", lambda event: self.load_profile_settings_callback( self.profile_var.get() ) ) # Trace variable for programmatic changes to also trigger load self.profile_var.trace_add( "write", lambda *args: self.load_profile_settings_callback( self.profile_var.get() ) ) # Save Settings Button self.save_settings_button = ttk.Button( self.profile_frame, text="Save Settings", command=self.save_profile_callback # Use controller's save method ) # Place button next to the dropdown self.save_settings_button.grid(row=0, column=2, sticky=tk.W, padx=(5, 2), pady=5) self.create_tooltip(self.save_settings_button, "Save current settings for selected profile.") # Add Profile Button self.add_profile_button = ttk.Button( self.profile_frame, text="Add", width=5, # Fixed small width command=self.add_profile_callback ) self.add_profile_button.grid(row=0, column=3, sticky=tk.W, padx=(2, 0), pady=5) # Remove Profile Button self.remove_profile_button = ttk.Button( self.profile_frame, text="Remove", width=8, # Slightly wider than Add command=self.remove_profile_callback ) self.remove_profile_button.grid(row=0, column=4, sticky=tk.W, padx=(2, 5), pady=5) def _populate_setup_tab(self): """Creates and places widgets for the Setup & Backup tab.""" parent_frame = self.setup_tab_frame # Create sub-frames within the tab for better organization # Pack them vertically, expanding horizontally repo_paths_frame = self._create_repo_paths_frame(parent_frame) repo_paths_frame.pack(pady=(0, 5), fill="x", expand=False) backup_config_frame = self._create_backup_config_frame(parent_frame) backup_config_frame.pack(pady=5, fill="x", expand=False) def _create_repo_paths_frame(self, parent): """Creates the sub-frame for repository paths and bundle names.""" frame = ttk.LabelFrame(parent, text="Repository & Bundle Paths", padding=(10, 5)) # Define columns for layout consistency col_label = 0 col_entry = 1 col_button = 2 col_indicator = 3 # Configure entry column (1) to expand horizontally frame.columnconfigure(col_entry, weight=1) # Row 0: SVN Path svn_label = ttk.Label(frame, text="SVN Working Copy:") svn_label.grid(row=0, column=col_label, sticky=tk.W, padx=5, pady=3) self.svn_path_entry = ttk.Entry(frame, width=60) self.svn_path_entry.grid(row=0, column=col_entry, sticky=tk.EW, padx=5, pady=3) # Bind events to trigger status updates self.svn_path_entry.bind( "", lambda e: self.update_svn_status_callback(self.svn_path_entry.get()) ) self.svn_path_entry.bind( "", lambda e: self.update_svn_status_callback(self.svn_path_entry.get()) ) self.svn_path_browse_button = ttk.Button( frame, text="Browse...", width=9, command=lambda: self.browse_folder_callback(self.svn_path_entry) ) self.svn_path_browse_button.grid(row=0, column=col_button, sticky=tk.W, padx=(0, 5), pady=3) # Status Indicator (Green/Red dot) self.svn_status_indicator = tk.Label( frame, text="", width=2, height=1, relief=tk.SUNKEN, background=self.RED, anchor=tk.CENTER ) self.svn_status_indicator.grid(row=0, column=col_indicator, sticky=tk.E, padx=(0, 5), pady=3) self.create_tooltip(self.svn_status_indicator, "Git repo status (Green=Ready, Red=Not Ready)") # Row 1: USB/Bundle Target Path usb_label = ttk.Label(frame, text="Bundle Target Dir:") usb_label.grid(row=1, column=col_label, sticky=tk.W, padx=5, pady=3) self.usb_path_entry = ttk.Entry(frame, width=60) self.usb_path_entry.grid(row=1, column=col_entry, sticky=tk.EW, padx=5, pady=3) self.usb_path_browse_button = ttk.Button( frame, text="Browse...", width=9, command=lambda: self.browse_folder_callback(self.usb_path_entry) ) self.usb_path_browse_button.grid(row=1, column=col_button, sticky=tk.W, padx=(0, 5), pady=3) # Row 2: Create Bundle Name create_label = ttk.Label(frame, text="Create Bundle Name:") create_label.grid(row=2, column=col_label, sticky=tk.W, padx=5, pady=3) self.bundle_name_entry = ttk.Entry(frame, width=60) # Span entry across entry and button columns self.bundle_name_entry.grid(row=2, column=col_entry, columnspan=2, sticky=tk.EW, padx=5, pady=3) # Row 3: Fetch Bundle Name fetch_label = ttk.Label(frame, text="Fetch Bundle Name:") fetch_label.grid(row=3, column=col_label, sticky=tk.W, padx=5, pady=3) self.bundle_updated_name_entry = ttk.Entry(frame, width=60) # Span entry across entry and button columns self.bundle_updated_name_entry.grid(row=3, column=col_entry, columnspan=2, sticky=tk.EW, padx=5, pady=3) return frame # Return the created frame def _create_backup_config_frame(self, parent): """Creates the sub-frame for backup configuration.""" frame = ttk.LabelFrame(parent, text="Backup Configuration (ZIP)", padding=(10, 5)) # Define columns col_label = 0 col_entry = 1 col_button = 2 # Configure entry column to expand frame.columnconfigure(col_entry, weight=1) # Row 0: Autobackup Checkbox self.autobackup_checkbox = ttk.Checkbutton( frame, text="Automatic Backup before Create/Fetch", variable=self.autobackup_var, command=self.toggle_backup_dir ) # Span checkbox across all columns self.autobackup_checkbox.grid(row=0, column=col_label, columnspan=3, sticky=tk.W, padx=5, pady=(5, 0)) # Row 1: Backup Directory Entry and Browse Button backup_dir_label = ttk.Label(frame, text="Backup Directory:") backup_dir_label.grid(row=1, column=col_label, sticky=tk.W, padx=5, pady=5) self.backup_dir_entry = ttk.Entry( frame, textvariable=self.backup_dir_var, width=60, state=tk.DISABLED ) self.backup_dir_entry.grid(row=1, column=col_entry, sticky=tk.EW, padx=5, pady=5) self.backup_dir_button = ttk.Button( frame, text="Browse...", width=9, command=self.browse_backup_dir, state=tk.DISABLED ) self.backup_dir_button.grid(row=1, column=col_button, sticky=tk.W, padx=(0, 5), pady=5) # Row 2: Exclude Extensions Entry exclude_label = ttk.Label(frame, text="Exclude Extensions:") exclude_label.grid(row=2, column=col_label, sticky=tk.W, padx=5, pady=5) self.backup_exclude_entry = ttk.Entry( frame, textvariable=self.backup_exclude_extensions_var, width=60 ) # Span entry across entry and button columns self.backup_exclude_entry.grid(row=2, column=col_entry, columnspan=2, sticky=tk.EW, padx=5, pady=5) self.create_tooltip(self.backup_exclude_entry, "Comma-separated extensions (e.g., .log,.tmp,.bak)") return frame # Return the created frame def _populate_commit_branch_tab(self): """Creates and places widgets for the Commit & Branches tab.""" parent_frame = self.commit_branch_tab_frame # Configure grid columns for overall tab layout parent_frame.columnconfigure(0, weight=1) # Column with listbox expands parent_frame.rowconfigure(1, weight=1) # Row with listbox expands vertically # --- Commit Section (Top) --- commit_subframe = self._create_commit_management_frame(parent_frame) commit_subframe.grid(row=0, column=0, columnspan=2, # Span both columns sticky="ew", padx=0, pady=(0, 10)) # --- Branch Section (Bottom) --- branch_subframe = self._create_branch_management_frame(parent_frame) branch_subframe.grid(row=1, column=0, columnspan=2, # Span both columns sticky="nsew", padx=0, pady=0) def _create_commit_management_frame(self, parent): """Creates the sub-frame for commit message and actions.""" frame = ttk.LabelFrame(parent, text="Commit", padding=5) # Configure internal columns frame.columnconfigure(1, weight=1) # Entry expands # Row 0: Autocommit Checkbox (for Create Bundle action) self.autocommit_checkbox = ttk.Checkbutton( frame, text="Autocommit before 'Create Bundle' (uses message below)", variable=self.autocommit_var, state=tk.DISABLED ) self.autocommit_checkbox.grid(row=0, column=0, columnspan=3, sticky="w", padx=5, pady=(5, 3)) self.create_tooltip(self.autocommit_checkbox, "If checked, commit changes using the message before Create Bundle.") # Row 1: Commit Message Entry + Manual Commit Button commit_msg_label = ttk.Label(frame, text="Commit Message:") commit_msg_label.grid(row=1, column=0, sticky="w", padx=5, pady=3) self.commit_message_entry = ttk.Entry( frame, textvariable=self.commit_message_var, width=50, state=tk.DISABLED ) self.commit_message_entry.grid(row=1, column=1, sticky="ew", padx=5, pady=3) self.create_tooltip(self.commit_message_entry, "Message for manual commit or autocommit.") self.commit_button = ttk.Button( frame, text="Commit Changes", width=15, command=self.manual_commit_callback, state=tk.DISABLED ) self.commit_button.grid(row=1, column=2, sticky="w", padx=(5, 0), pady=3) self.create_tooltip(self.commit_button, "Manually commit staged changes with this message.") return frame # Return the created frame def _create_branch_management_frame(self, parent): """Creates the sub-frame for branch operations.""" frame = ttk.LabelFrame(parent, text="Branches", padding=5) # Configure grid columns within this frame frame.columnconfigure(0, weight=1) # Listbox column expands frame.rowconfigure(2, weight=1) # Listbox row expands # Row 0: Current Branch Display current_branch_label = ttk.Label(frame, text="Current Branch:") current_branch_label.grid(row=0, column=0, sticky="w", padx=5, pady=3) self.current_branch_display = ttk.Label( frame, textvariable=self.current_branch_var, font=("Segoe UI", 9, "bold"), relief=tk.SUNKEN, padding=(3, 1) ) # Span display across listbox and button columns? Or just listbox? self.current_branch_display.grid(row=0, column=1, columnspan=2, # Span 2 sticky="ew", padx=5, pady=3) self.create_tooltip(self.current_branch_display, "The currently active branch or state.") # Row 1: Listbox Label branch_list_label = ttk.Label(frame, text="Local Branches:") branch_list_label.grid(row=1, column=0, columnspan=3, # Span all columns sticky="w", padx=5, pady=(10, 0)) # Row 2: Listbox + Scrollbar Frame (Spans first 3 columns) branch_list_frame = ttk.Frame(frame) branch_list_frame.grid(row=2, column=0, columnspan=3, sticky="nsew", padx=5, pady=(0, 5)) branch_list_frame.rowconfigure(0, weight=1) branch_list_frame.columnconfigure(0, weight=1) self.branch_listbox = tk.Listbox( branch_list_frame, height=5, exportselection=False, selectmode=tk.SINGLE, font=("Consolas", 9) ) self.branch_listbox.grid(row=0, column=0, sticky="nsew") branch_scrollbar = ttk.Scrollbar( branch_list_frame, orient=tk.VERTICAL, command=self.branch_listbox.yview ) branch_scrollbar.grid(row=0, column=1, sticky="ns") self.branch_listbox.config(yscrollcommand=branch_scrollbar.set) self.create_tooltip(self.branch_listbox, "Select a branch for actions (Switch, Delete).") # Row 2, Column 3: Vertical Button Frame for Branch Actions branch_button_frame = ttk.Frame(frame) branch_button_frame.grid(row=2, column=3, sticky="ns", # North-South align padx=(10, 5), pady=(0, 5)) # Add left padding button_width_branch = 18 # Consistent width self.refresh_branches_button = ttk.Button( branch_button_frame, text="Refresh List", width=button_width_branch, command=self.refresh_branches_callback, state=tk.DISABLED ) self.refresh_branches_button.pack(side=tk.TOP, fill=tk.X, pady=(0, 3)) self.create_tooltip(self.refresh_branches_button, "Reload branch list.") self.create_branch_button = ttk.Button( branch_button_frame, text="Create Branch...", width=button_width_branch, command=self.create_branch_callback, state=tk.DISABLED ) self.create_branch_button.pack(side=tk.TOP, fill=tk.X, pady=3) self.create_tooltip(self.create_branch_button, "Create a new local branch.") self.switch_branch_button = ttk.Button( branch_button_frame, text="Switch to Selected", width=button_width_branch, command=self.switch_branch_callback, state=tk.DISABLED ) self.switch_branch_button.pack(side=tk.TOP, fill=tk.X, pady=3) self.create_tooltip(self.switch_branch_button, "Checkout selected branch.") self.delete_branch_button = ttk.Button( branch_button_frame, text="Delete Selected", width=button_width_branch, command=self.delete_branch_callback, state=tk.DISABLED ) self.delete_branch_button.pack(side=tk.TOP, fill=tk.X, pady=(3, 0)) self.create_tooltip(self.delete_branch_button, "Delete selected local branch.") return frame # Return the created frame def _populate_tags_gitignore_tab(self): """Creates and places widgets for the Tags & Gitignore tab.""" parent_frame = self.tags_gitignore_tab_frame # Configure grid parent_frame.columnconfigure(0, weight=1) # Listbox expands parent_frame.rowconfigure(0, weight=1) # Listbox expands vertically # --- Tag Listing Area --- tag_list_frame = ttk.LabelFrame(parent_frame, text="Tags", padding=5) tag_list_frame.grid(row=0, column=0, sticky="nsew", padx=5, pady=5) tag_list_frame.rowconfigure(0, weight=1) tag_list_frame.columnconfigure(0, weight=1) self.tag_listbox = tk.Listbox( tag_list_frame, height=8, exportselection=False, selectmode=tk.SINGLE, font=("Consolas", 9) ) self.tag_listbox.grid(row=0, column=0, sticky="nsew") tag_scrollbar = ttk.Scrollbar( tag_list_frame, orient=tk.VERTICAL, command=self.tag_listbox.yview ) tag_scrollbar.grid(row=0, column=1, sticky="ns") self.tag_listbox.config(yscrollcommand=tag_scrollbar.set) self.create_tooltip(self.tag_listbox, "Tags (newest first) with messages. Select for actions.") # --- Tag/Gitignore Action Buttons Area --- (Vertical column) tag_button_frame = ttk.Frame(parent_frame) tag_button_frame.grid(row=0, column=1, rowspan=2, # Span rows potentially sticky="ns", padx=(0, 5), pady=5) button_width_tag = 18 # Consistent width self.refresh_tags_button = ttk.Button( tag_button_frame, text="Refresh Tags", width=button_width_tag, command=self.refresh_tags_callback, state=tk.DISABLED ) self.refresh_tags_button.pack(side=tk.TOP, fill=tk.X, pady=(0, 3)) self.create_tooltip(self.refresh_tags_button, "Reload tag list.") self.create_tag_button = ttk.Button( tag_button_frame, text="Create Tag...", width=button_width_tag, command=self.create_tag_callback, state=tk.DISABLED ) self.create_tag_button.pack(side=tk.TOP, fill=tk.X, pady=3) self.create_tooltip(self.create_tag_button, "Commit changes (if message provided) & create tag.") self.checkout_tag_button = ttk.Button( tag_button_frame, text="Checkout Selected Tag", width=button_width_tag, command=self.checkout_tag_callback, state=tk.DISABLED ) self.checkout_tag_button.pack(side=tk.TOP, fill=tk.X, pady=3) self.create_tooltip(self.checkout_tag_button, "Switch to selected tag (Detached HEAD).") # --- ADDED: Delete Tag Button --- self.delete_tag_button = ttk.Button( tag_button_frame, text="Delete Selected Tag", width=button_width_tag, command=self.delete_tag_callback, state=tk.DISABLED ) self.delete_tag_button.pack(side=tk.TOP, fill=tk.X, pady=3) self.create_tooltip(self.delete_tag_button, "Delete the selected tag locally.") # Edit .gitignore button (also in this column) self.edit_gitignore_button = ttk.Button( tag_button_frame, text="Edit .gitignore", width=button_width_tag, command=self.open_gitignore_editor_callback, state=tk.DISABLED ) self.edit_gitignore_button.pack(side=tk.TOP, fill=tk.X, pady=(3, 0)) self.create_tooltip(self.edit_gitignore_button, "Open editor for the .gitignore file.") def _create_function_frame(self): """Creates the frame holding the Core Action buttons (below tabs).""" self.function_frame = ttk.LabelFrame( self, text="Core Actions", padding=(10, 10) ) self.function_frame.pack(pady=(5, 5), fill="x", anchor=tk.N) # Sub-frame to center the buttons horizontally button_subframe = ttk.Frame(self.function_frame) button_subframe.pack() # Default pack behavior centers horizontally self.prepare_svn_button = ttk.Button( button_subframe, text="Prepare SVN Repo", command=self.prepare_svn_for_git_callback ) self.prepare_svn_button.pack(side=tk.LEFT, padx=(0,5), pady=5) self.create_bundle_button = ttk.Button( button_subframe, text="Create Bundle", command=self.create_git_bundle_callback ) self.create_bundle_button.pack(side=tk.LEFT, padx=5, pady=5) self.fetch_bundle_button = ttk.Button( button_subframe, text="Fetch from Bundle", command=self.fetch_from_git_bundle_callback ) self.fetch_bundle_button.pack(side=tk.LEFT, padx=5, pady=5) self.manual_backup_button = ttk.Button( button_subframe, text="Backup Now (ZIP)", command=self.manual_backup_callback ) self.manual_backup_button.pack(side=tk.LEFT, padx=5, pady=5) def _create_log_area(self): """Creates the scrolled text area for logging output.""" log_frame = ttk.Frame(self.master) # Attach to root window # Pack at the very bottom, allow expansion log_frame.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True, padx=10, pady=(0, 10)) # Padding only below self.log_text = scrolledtext.ScrolledText( log_frame, height=8, width=100, # Adjusted height font=("Consolas", 9), wrap=tk.WORD, state=tk.DISABLED ) self.log_text.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True) def _initialize_profile_selection(self): """Sets the initial value of the profile dropdown.""" try: from config_manager import DEFAULT_PROFILE except ImportError: DEFAULT_PROFILE = "default" # Fallback if DEFAULT_PROFILE in self.initial_profile_sections: self.profile_var.set(DEFAULT_PROFILE) elif self.initial_profile_sections: self.profile_var.set(self.initial_profile_sections[0]) # else: profile_var remains empty # --- GUI Update Methods --- def toggle_backup_dir(self): """Enables/disables backup directory widgets based on checkbox.""" new_state = tk.NORMAL if self.autobackup_var.get() else tk.DISABLED if hasattr(self, 'backup_dir_entry'): self.backup_dir_entry.config(state=new_state) if hasattr(self, 'backup_dir_button'): self.backup_dir_button.config(state=new_state) def browse_backup_dir(self): """Opens a directory selection dialog for backup directory.""" initial = self.backup_dir_var.get() or DEFAULT_BACKUP_DIR dirname = filedialog.askdirectory(initialdir=initial, title="Select Backup Dir", parent=self.master) if dirname: self.backup_dir_var.set(dirname) def update_svn_indicator(self, is_prepared): """Updates only the indicator color and Prepare button state.""" color = self.GREEN if is_prepared else self.RED state = tk.DISABLED if is_prepared else tk.NORMAL tip = "Repo Prepared" if is_prepared else "Repo Not Prepared" if hasattr(self, 'svn_status_indicator'): self.svn_status_indicator.config(background=color) self.update_tooltip(self.svn_status_indicator, tip) if hasattr(self, 'prepare_svn_button'): self.prepare_svn_button.config(state=state) def update_profile_dropdown(self, sections): """Updates the profile combobox list.""" if hasattr(self, 'profile_dropdown'): current = self.profile_var.get() self.profile_dropdown['values'] = sections # Maintain selection logic if sections: if current in sections: self.profile_var.set(current) elif "default" in sections: self.profile_var.set("default") else: self.profile_var.set(sections[0]) else: self.profile_var.set("") def update_tag_list(self, tags_with_subjects): """Clears and repopulates tag listbox with name and subject.""" if not hasattr(self, 'tag_listbox'): logging.error("Tag listbox missing for update.") return try: self.tag_listbox.delete(0, tk.END) if tags_with_subjects: # Reset color if needed try: if self.tag_listbox.cget("fg") == "grey": self.tag_listbox.config(fg='SystemWindowText') except tk.TclError: pass # Ignore color errors # Insert items for name, subject in tags_with_subjects: display = f"{name}\t({subject})" # Tab separation self.tag_listbox.insert(tk.END, display) else: # Show placeholder self.tag_listbox.insert(tk.END, "(No tags found)") try: self.tag_listbox.config(fg="grey") except tk.TclError: pass # Ignore color errors except tk.TclError as e: logging.error(f"TclError updating tags: {e}") except Exception as e: logging.error(f"Error updating tags: {e}", exc_info=True) def get_selected_tag(self): """Returns the name only of the selected tag.""" if hasattr(self, 'tag_listbox'): indices = self.tag_listbox.curselection() if indices: item = self.tag_listbox.get(indices[0]) if item != "(No tags found)": # Get text before the first tab tag_name = item.split('\t', 1)[0] return tag_name.strip() return None # No selection or invalid item def update_branch_list(self, branches): """Clears and repopulates the branch listbox.""" if not hasattr(self, 'branch_listbox'): logging.error("Branch listbox missing for update.") return try: current = self.current_branch_var.get() # Get displayed current branch self.branch_listbox.delete(0, tk.END) if branches: # Reset color if needed try: if self.branch_listbox.cget("fg") == "grey": self.branch_listbox.config(fg='SystemWindowText') except tk.TclError: pass # Insert branches, highlight current for branch in branches: is_current = (branch == current) # Add '*' prefix for current branch display display = f"* {branch}" if is_current else f" {branch}" self.branch_listbox.insert(tk.END, display) # Highlight current branch in the list if is_current: self.branch_listbox.itemconfig( tk.END, {'fg': 'blue', 'selectbackground': 'lightblue'} ) else: # Show placeholder if no branches self.branch_listbox.insert(tk.END, "(No local branches?)") try: self.branch_listbox.config(fg="grey") except tk.TclError: pass except tk.TclError as e: logging.error(f"TclError updating branches: {e}") except Exception as e: logging.error(f"Error updating branches: {e}", exc_info=True) def get_selected_branch(self): """Returns the name only of the selected branch.""" if hasattr(self, 'branch_listbox'): indices = self.branch_listbox.curselection() if indices: item = self.branch_listbox.get(indices[0]) # Remove potential '*' prefix and leading/trailing whitespace return item.lstrip("* ").strip() return None # No selection def set_current_branch_display(self, branch_name): """Updates the label showing the current branch.""" if hasattr(self, 'current_branch_var'): # Set display text, handling None or empty string display_text = branch_name if branch_name else "(DETACHED or N/A)" self.current_branch_var.set(display_text) # --- Dialog Wrappers --- def ask_new_profile_name(self): """Asks the user for a new profile name.""" return simpledialog.askstring("Add Profile", "Enter new profile name:", parent=self.master) def show_error(self, title, message): """Displays an error message box.""" messagebox.showerror(title, message, parent=self.master) def show_info(self, title, message): """Displays an information message box.""" messagebox.showinfo(title, message, parent=self.master) def show_warning(self, title, message): """Displays a warning message box.""" messagebox.showwarning(title, message, parent=self.master) def ask_yes_no(self, title, message): """Displays a yes/no confirmation dialog.""" return messagebox.askyesno(title, message, parent=self.master) # --- Tooltip Helpers --- def create_tooltip(self, widget, text): """Creates a tooltip for a given widget.""" tooltip = Tooltip(widget, text) # Use add='+' to avoid overwriting other bindings widget.bind("", lambda e, tt=tooltip: tt.showtip(), add='+') widget.bind("", lambda e, tt=tooltip: tt.hidetip(), add='+') # Hide tooltip also when clicking the widget widget.bind("", lambda e, tt=tooltip: tt.hidetip(), add='+') def update_tooltip(self, widget, text): """Updates the text of an existing tooltip (by re-creating it).""" # Simple approach: Remove old bindings and create new tooltip widget.unbind("") widget.unbind("") widget.unbind("") # Re-create the tooltip with the new text self.create_tooltip(widget, text)