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

Allow fixing window position to any side #763

Merged
merged 3 commits into from
Oct 17, 2021

Conversation

Timoses
Copy link
Contributor

@Timoses Timoses commented Aug 13, 2021

Implements ability to configure which edge fixes the windows position.

This is valuable when resizing the window dynamically so that the user can decide which position of the window is remains fixed.

Implemented platforms so far:

  • Cocoa
  • qt
  • gtk (not tested)

Missing implementation:

  • cef
  • winforms

Further, it should be possible to dynamically adjust the gravity point / fix position of the window during runtime.
Hence, I believe there should be a method available on webview.window.Window to set it.

Do you have any valuable insight and suggestions?

@Timoses Timoses force-pushed the feature/fix-window-positions branch from e2f6fa0 to 0754fe6 Compare August 25, 2021 15:49
@Timoses Timoses force-pushed the feature/fix-window-positions branch from 0754fe6 to 74f8b4d Compare August 26, 2021 12:51
@Timoses
Copy link
Contributor Author

Timoses commented Aug 26, 2021

@r0x0r What do you think?

@r0x0r
Copy link
Owner

r0x0r commented Sep 2, 2021

Sorry for the late response. I have tested this with

    window = webview.create_window('Simple browser', 'https://pywebview.flowrl.com', fix_point=webview.FixWindow.SOUTH)
    webview.start()

and could not see any difference. How should it behave?

@Timoses
Copy link
Contributor Author

Timoses commented Sep 2, 2021

Sorry for the late response. I have tested this with

    window = webview.create_window('Simple browser', 'https://pywebview.flowrl.com', fix_point=webview.FixWindow.SOUTH)
    webview.start()

and could not see any difference. How should it behave?

Ty for taking a look.

The effect should come into play when you dynamically resize the window (https://github.com/r0x0r/pywebview/blob/master/webview/window.py#L240).

Default is that the top left corner of the window stays on the same position. There are however scenarios when other sides should stay fixed.

An example would be a notification window with variable size which changes over time. If the window were positioned on the right side of the screen and grew then in the current setup it would potentially grow over the right side of the screen. With this PR it can be set to be fixed on the 'EAST' side so that the window grows towards the left side.

@Timoses
Copy link
Contributor Author

Timoses commented Sep 2, 2021

Example - should give you a steadily growing window which you can move around (not tested):

def adjust_loop(window, screen_w_2, screen_h_2):
    x = 0
    y = 0
    width = 0
    height = 0

    while True:
        time.sleep(0.5)
        if window.x < screen_w_2:
            window.load_css('p { text-align: left; }')
            # Currently in PR https://github.com/r0x0r/pywebview/pull/763
            if hasattr('window', 'fix_point'):
                window.fix_point = window.fix_point & (~ webview.FixWindow.EAST)
        else:
            window.load_css('p { text-align: right; }')
            # Currently in PR https://github.com/r0x0r/pywebview/pull/763
            if hasattr('window', 'fix_point'):
                window.fix_point = window.fix_point | webview.FixWindow.EAST

        if window.y < screen_h_2:
            # Currently in PR https://github.com/r0x0r/pywebview/pull/763
            if hasattr('window', 'fix_point'):
                window.fix_point = window.fix_point & (~ webview.FixWindow.SOUTH)
        else:
            # Currently in PR https://github.com/r0x0r/pywebview/pull/763
            if hasattr('window', 'fix_point'):
                window.fix_point = window.fix_point | webview.FixWindow.SOUTH

        width = width + 1
        height = height + 1
        window.resize(width+10, height+10)

def start_me():
    window = webview.create_window('Woah dude!', url=https://github.com/r0x0r/pywebview/pull/763, on_top=True,
                                   frameless=True, width=500, height=500,
                                   transparent=False, resizable=False)
    screen_w = webview.screens[0].width / 2
    screen_h = webview.screens[0].height / 2
    webview.start(adjust_loop, (window, screen_w, screen_h))

@r0x0r
Copy link
Owner

r0x0r commented Sep 6, 2021

Thanks for the explanation. I see what this is all about now.
What do other platforms look like? Would you be able to implement this feature for all the platforms?
If no native support for this feature is available on other platforms, it can be mimicked by adjusting both dimensions and position of the window.

Another suggestion is it would be better to have this as an additional optional parameter for to the resize method, eg. window.resize(100, 200, FixPoint.SOUTH)

@Timoses
Copy link
Contributor Author

Timoses commented Sep 8, 2021

Thanks @r0x0r for the input.
I pushed another commit implementing your suggestion.

Also implemented platform gtk. However i did not test it.

As to winforms and cef I did not find proper documentation regarding windll.user32.SetWindowPos.

@r0x0r
Copy link
Owner

r0x0r commented Sep 10, 2021

The documentation for SetWindowPos can be found here
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos

@Timoses
Copy link
Contributor Author

Timoses commented Sep 23, 2021

Finally came around to an attempt to implement remaining platforms.

I did not test it. Also cef implementation is not complete.

@r0x0r could you please have a look at it?

move_y = 0

rect = windll.?.RECT()
rect = windll.user32.GetWindowRect(self.inner_hwnd, rect)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@r0x0r How would I do this correctly? Do you know?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CEF code is not needed at all. The only thing the CEF code does is resizing of the CEF component. Its position always stays the same relative to the parent window.

@r0x0r r0x0r merged commit 9915792 into r0x0r:master Oct 17, 2021
@Timoses Timoses changed the title [WIP] Allow fixing window position to any side Allow fixing window position to any side Nov 17, 2021
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

Successfully merging this pull request may close these issues.

2 participants