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

Always on Top can't be disabled #1848

Closed
Mstpyt opened this issue Jul 28, 2022 · 1 comment · Fixed by #1849
Closed

Always on Top can't be disabled #1848

Mstpyt opened this issue Jul 28, 2022 · 1 comment · Fixed by #1849
Labels
state: pending not addressed yet type: bug bug

Comments

@Mstpyt
Copy link
Collaborator

Mstpyt commented Jul 28, 2022

Version of Dear PyGui

Version: 16.3.
Operating System: Win10

My Issue/Question

dpg.always_on_top can't be disabled on windows

A clear and concise description of what the issue/question is. Please provide as much context as possible.
-- already fixed with next PR.

To Reproduce

Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior

A clear and concise description of what you expected to happen.

Screenshots/Video

XXX (you can drag files here)

Standalone, minimal, complete and verifiable example

import dearpygui.dearpygui as dpg

def toggleOnTop(item: int, val: bool):
    print("Before:", dpg.is_viewport_always_top())
    dpg.set_viewport_always_top(val)
    print("After:", dpg.is_viewport_always_top())

dpg.create_context()

with dpg.window(tag="Primary"):
    with dpg.tab_bar():
        with dpg.tab(label="On Top"):
            dpg.add_checkbox(label="Always On Top", callback=toggleOnTop)

dpg.create_viewport(title="On Top test")
dpg.set_primary_window("Primary", True)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
@IvanNazaruk
Copy link

This issue has not yet been fixed. I suggest using my way with ctypes:

import os
from ctypes import WINFUNCTYPE, WinDLL, Structure, c_long, byref, wintypes

user32 = WinDLL("user32")
user32.SetWindowPos.restype = wintypes.BOOL
user32.SetWindowPos.argtypes = [wintypes.HWND, wintypes.HWND, wintypes.INT, wintypes.INT, wintypes.INT, wintypes.INT, wintypes.UINT, ]
WNDENUMPROC = WINFUNCTYPE(wintypes.BOOL,
                          wintypes.HWND,
                          wintypes.LPARAM)
user32.EnumWindows.argtypes = [WNDENUMPROC,
                               wintypes.LPARAM]


def get_hwnd_from_pid(pid: int) -> int | None:
    import ctypes
    result = None

    def callback(hwnd, _):
        nonlocal result
        lpdw_PID = ctypes.c_ulong()
        user32.GetWindowThreadProcessId(hwnd, ctypes.byref(lpdw_PID))
        hwnd_PID = lpdw_PID.value

        if hwnd_PID == pid:
            result = hwnd
            return False
        return True

    cb_worker = WNDENUMPROC(callback)
    user32.EnumWindows(cb_worker, 0)
    return result


def set_always_top(value: bool):
    # window = user32.GetForegroundWindow()
    window = get_hwnd_from_pid(os.getpid())
    if window is None:
        return

    class RECT(Structure):
        _fields_ = [
            ('left', c_long),
            ('top', c_long),
            ('right', c_long),
            ('bottom', c_long),
        ]

        def width(self):  return self.right - self.left

        def height(self): return self.bottom - self.top

    rc = RECT()
    user32.GetWindowRect(window, byref(rc))
    value = -1 if value else -2
    user32.SetWindowPos(window, value, rc.left, rc.top, 0, 0, 0x0001)


import dearpygui.dearpygui as dpg

dpg.create_context()
with dpg.window():
    dpg.add_checkbox(label='Always On Top', callback=lambda _, value: set_always_top(value))

dpg.create_viewport()
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

hoffstadt pushed a commit that referenced this issue Oct 22, 2022
* Fix for 1716

* Fix für 1795

* Fix for Issue 1848

Co-authored-by: mstuder <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: pending not addressed yet type: bug bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants