Untitled

 avatar
unknown
plain_text
5 months ago
6.9 kB
3
Indexable
class SettingsMenu(ctk.CTkFrame):
    def __init__(self, parent, dark_mode):
        # Calculate window dimensions
        window_width = parent.winfo_width()
        window_height = parent.winfo_height()
        menu_width = max(window_width // 4, 256)  # 1/4 of window width or minimum 256px

        # Initialize with required dimensions
        super().__init__(
            parent, 
            fg_color='#1c1c1e' if dark_mode else '#f5f5f7',
            width=menu_width,
            height=window_height
        )

        self.parent = parent
        self.dark_mode = dark_mode
        self.is_open = False
        self.animation_running = False
        self.current_width = 0
        self.target_width = menu_width

        # Place the frame initially off-screen
        self.place(relx=1, rely=0, x=menu_width, y=0, anchor='ne')

        # Add placeholder content
        self.create_menu_content()

        # Create settings button
        try:
            is_light_bg = not dark_mode
            icon_suffix = "-b" if is_light_bg else "-w"

            icon_image = ctk.CTkImage(
                light_image=Image.open(f"icons/settings{icon_suffix}.png"),
                dark_image=Image.open(f"icons/settings{icon_suffix}.png"),
                size=(30, 30)
            )
            self.settings_button = ctk.CTkButton(
                self.parent,
                image=icon_image,
                text="",
                width=30,
                height=30,
                fg_color="transparent",
                hover_color='#2c2c2e' if dark_mode else '#e5e5e7',
                command=self.toggle_menu
            )
            self.settings_button.place(relx=0.97, rely=0.04, anchor='center')

        except Exception as e:
            print(f"Error loading settings icon: {e}")
            self.settings_button = ctk.CTkButton(
                self.parent,
                text="âš™",
                width=30,
                height=30,
                fg_color="transparent",
                hover_color='#2c2c2e' if dark_mode else '#e5e5e7',
                command=self.toggle_menu
            )
            self.settings_button.place(relx=0.97, rely=0.04, anchor='center')

        # Create overlay frame with a compatible semi-transparent color
        overlay_color = '#000000' if dark_mode else '#000000'
        self.overlay = ctk.CTkFrame(
            self.parent,
            fg_color=overlay_color,
            corner_radius=0,
            width=window_width,
            height=window_height
        )

        # Bind click event on the overlay to close the menu
        self.overlay.bind("<Button-1>", self.close_menu)

    def create_menu_content(self):
        """Create the content of the settings menu"""
        # Title
        title = ctk.CTkLabel(
            self,
            text="Settings",
            font=("Dongle", 24, "bold"),
            text_color='white' if self.dark_mode else 'black'
        )
        title.pack(pady=(20, 10), padx=20, anchor='w')

        # Create a frame for the placeholder content
        content_frame = ctk.CTkFrame(
            self,
            fg_color="transparent",
        )
        content_frame.pack(fill='both', expand=True, padx=20, pady=10)

        # Add placeholder sections with separators
        placeholders = [
            "General Settings",
            "User Preferences",
            "System Configuration",
            "Display Options",
            "Language Settings",
            "Security Settings",
            "About"
        ]

        for i, text in enumerate(placeholders):
            # Add separator before text (except for first item)
            if i > 0:
                separator = ctk.CTkFrame(
                    content_frame,
                    height=2,
                    fg_color='#2c2c2e' if self.dark_mode else '#e5e5e7'
                )
                separator.pack(fill='x', pady=(15, 15))

            # Add placeholder text
            label = ctk.CTkLabel(
                content_frame,
                text=text,
                font=("Dongle", 18),
                text_color='white' if self.dark_mode else 'black'
            )
            label.pack(anchor='w', pady=5)

    def toggle_menu(self):
        """Toggle the settings menu open or closed"""
        if not self.animation_running:
            if self.is_open:
                self.close_menu()
            else:
                self.open_menu()

    def open_menu(self, event=None):
        """Open the settings menu"""
        if not self.is_open and not self.animation_running:
            self.is_open = True
            self.animation_running = True

            # Show overlay
            self.overlay.place(x=0, y=0)
            self.lift()  # Bring menu to front
            self.animate_open()

            # Deactivate all main window buttons
            self.disable_main_buttons()

    def close_menu(self, event=None):
        """Close the settings menu"""
        if self.is_open and not self.animation_running:
            self.is_open = False
            self.animation_running = True
            self.overlay.place_forget()
            self.animate_closed()

            # Reactivate all main window buttons
            self.enable_main_buttons()

    def animate_open(self):
        """Animate the opening of the settings menu"""
        if self.current_width < self.target_width:
            self.current_width += 40
            x_pos = self.target_width - self.current_width
            self.place(relx=1, rely=0, x=x_pos, y=0, anchor='ne')
            self.after(16, self.animate_open)
        else:
            self.current_width = self.target_width
            self.animation_running = False

    def animate_closed(self):
        """Animate the closing of the settings menu"""
        if self.current_width > 0:
            self.current_width -= 40
            x_pos = self.target_width - self.current_width
            self.place(relx=1, rely=0, x=x_pos, y=0, anchor='ne')
            self.after(16, self.animate_closed)
        else:
            self.current_width = 0
            self.animation_running = False
            self.place(relx=1, rely=0, x=self.target_width, y=0, anchor='ne')

    def disable_main_buttons(self):
        """Disable all buttons in the main window"""
        for widget in self.parent.winfo_children():
            if isinstance(widget, ctk.CTkButton):
                widget.configure(state="disabled")

    def enable_main_buttons(self):
        """Enable all buttons in the main window"""
        for widget in self.parent.winfo_children():
            if isinstance(widget, ctk.CTkButton):
                widget.configure(state="normal")
Editor is loading...
Leave a Comment