Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dark Title Bar In Windows #2195

Closed
dipeshSam opened this issue Dec 5, 2023 · 0 comments
Closed

Dark Title Bar In Windows #2195

dipeshSam opened this issue Dec 5, 2023 · 0 comments

Comments

@dipeshSam
Copy link

dipeshSam commented Dec 5, 2023

While it may deviate from the core philosophy of Flet building, the integration of a darkened title bar, a standard feature within Windows, notably in Windows 11, is within the realm of possibility for developers. This functionality holds the potential to attract a broader audience, particularly among fervent Windows enthusiasts. In the context of Tkinter, specifically customtkinter, as well as other versatile GUI libraries and the C++ development environment for Windows, various mechanisms exist to facilitate the darkening of the title bar.

In customtkinter, and other same libraries can make the title bar dark using following appropriate approach:

def _windows_set_titlebar_color(self, color_mode: str):
        """
        Set the titlebar color of the window to light or dark theme on Microsoft Windows.

        Credits for this function:
        https://stackoverflow.com/questions/23836000/can-i-change-the-title-bar-in-tkinter/70724666#70724666

        MORE INFO:
        https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
        """

        if sys.platform.startswith("win") and not self._deactivate_windows_window_header_manipulation:

            if self._window_exists:
                self._state_before_windows_set_titlebar_color = self.state()
                # print("window_exists -> state_before_windows_set_titlebar_color: ", self.state_before_windows_set_titlebar_color)

                if self._state_before_windows_set_titlebar_color != "iconic" or self._state_before_windows_set_titlebar_color != "withdrawn":
                    self.focused_widget_before_widthdraw = self.focus_get()
                    super().withdraw()  # hide window so that it can be redrawn after the titlebar change so that the color change is visible
            else:
                # print("window dont exists -> withdraw and update")
                self.focused_widget_before_widthdraw = self.focus_get()
                super().withdraw()
                super().update()

            if color_mode.lower() == "dark":
                value = 1
            elif color_mode.lower() == "light":
                value = 0
            else:
                return

            try:
                hwnd = ctypes.windll.user32.GetParent(self.winfo_id())
                DWMWA_USE_IMMERSIVE_DARK_MODE = 20
                DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19

                # try with DWMWA_USE_IMMERSIVE_DARK_MODE
                if ctypes.windll.dwmapi.DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE,
                                                              ctypes.byref(ctypes.c_int(value)),
                                                              ctypes.sizeof(ctypes.c_int(value))) != 0:

                    # try with DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20h1
                    ctypes.windll.dwmapi.DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1,
                                                               ctypes.byref(ctypes.c_int(value)),
                                                               ctypes.sizeof(ctypes.c_int(value)))

            except Exception as err:
                print(err)

            if self._window_exists or True:
                # print("window_exists -> return to original state: ", self.state_before_windows_set_titlebar_color)
                if self._state_before_windows_set_titlebar_color == "normal":
                    self.deiconify()
                elif self._state_before_windows_set_titlebar_color == "iconic":
                    self.iconify()
                elif self._state_before_windows_set_titlebar_color == "zoomed":
                    self.state("zoomed")
                else:
                    self.state(self._state_before_windows_set_titlebar_color)  # other states
            else:
                pass  # wait for update or mainloop to be called

            if self.focused_widget_before_widthdraw is not None:
                self.after(1, self.focused_widget_before_widthdraw.focus)
                self.focused_widget_before_widthdraw = None
zrr1999 pushed a commit to zrr1999/flet that referenced this issue Jul 17, 2024
* Migrate Windows client to support dark window title

Fix flet-dev#2195

* Fixed Windows client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant