Skip to content

Commit

Permalink
Redraw window immediately on "live resizing"
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Jan 13, 2021
1 parent 74fbd0a commit 39ff912
Show file tree
Hide file tree
Showing 22 changed files with 152 additions and 291 deletions.
9 changes: 8 additions & 1 deletion examples/helloworld.cpp
@@ -1,5 +1,5 @@
// LAF Library
// Copyright (c) 2019-2020 Igara Studio S.A.
// Copyright (c) 2019-2021 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand Down Expand Up @@ -39,6 +39,7 @@ int app_main(int argc, char* argv[])

display->setNativeMouseCursor(os::kArrowCursor);
display->setTitle("Hello World");
display->handleResize = draw_display;

// On macOS: With finishLaunching() we start processing
// NSApplicationDelegate events. After calling this we'll start
Expand Down Expand Up @@ -92,6 +93,12 @@ int app_main(int argc, char* argv[])
display->setScale(1 + (int)(ev.scancode() - os::kKey1));
redraw = true;
break;

case os::kKeyF:
case os::kKeyF11:
display->setFullscreen(!display->isFullscreen());
break;

default:
// Do nothing
break;
Expand Down
1 change: 0 additions & 1 deletion os/CMakeLists.txt
Expand Up @@ -59,7 +59,6 @@ if(LAF_BACKEND STREQUAL "skia")
endif()
list(APPEND LAF_OS_SOURCES
skia/os.cpp
skia/resize_surface.cpp
skia/skia_color_space.cpp
skia/skia_display.cpp
skia/skia_draw_text.cpp
Expand Down
91 changes: 0 additions & 91 deletions os/common/event_queue_with_resize_display.h

This file was deleted.

11 changes: 10 additions & 1 deletion os/display.h
@@ -1,5 +1,5 @@
// LAF OS Library
// Copyright (c) 2018-2020 Igara Studio S.A.
// Copyright (c) 2018-2021 Igara Studio S.A.
// Copyright (c) 2012-2018 David Capello
//
// This file is released under the terms of the MIT license.
Expand All @@ -15,6 +15,7 @@
#include "os/ref.h"
#include "os/surface_list.h"

#include <functional>
#include <string>

namespace os {
Expand All @@ -30,6 +31,14 @@ namespace os {

virtual ~Display() { }

// Function called to handle a "live resize"/resizing loop. If
// this is nullptr, an Event::ResizeDisplay is generated when the
// resizing is finished.
//
// TODO I think we should have a DisplayDelegate for this instead
// of a public property.
std::function<void(os::Display*)> handleResize = nullptr;

// Returns the real and current display's size (without scale applied).
virtual int width() const = 0;
virtual int height() const = 0;
Expand Down
3 changes: 2 additions & 1 deletion os/osx/window.h
@@ -1,5 +1,5 @@
// LAF OS Library
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2012-2017 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -36,6 +36,7 @@ class OSXWindowImpl {
virtual void onDrawRect(const gfx::Rect& rect) = 0;
virtual void onWindowChanged() = 0;
virtual void onStartResizing() = 0;
virtual void onResizing(gfx::Size& size) = 0;
virtual void onEndResizing() = 0;

// This generally happens when the window is moved to another
Expand Down
6 changes: 5 additions & 1 deletion os/osx/window.mm
@@ -1,5 +1,5 @@
// LAF OS Library
// Copyright (C) 2020 Igara Studio S.A.
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2012-2017 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -51,6 +51,10 @@ - (OSXWindow*)initWithImpl:(OSXWindowImpl*)impl
OSXView* view = [[OSXView alloc] initWithFrame:rect];
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

// Redraw the entire window content when we resize it.
// TODO add support to avoid redrawing the entire window
self.preservesContentDuringLiveResize = false;

[self setDelegate:m_delegate];
[self setContentView:view];
[self center];
Expand Down
11 changes: 10 additions & 1 deletion os/osx/window_delegate.h
@@ -1,5 +1,5 @@
// LAF OS Library
// Copyright (C) 2020 Igara Studio S.A.
// Copyright (C) 2020-2021 Igara Studio S.A.
// Copyright (C) 2015 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -41,6 +41,15 @@ class OSXWindowImpl;
m_impl->onStartResizing();
}

- (NSSize)windowWillResize:(NSWindow*)sender
toSize:(NSSize)frameSize
{
NSView* view = sender.contentView;
gfx::Size sz(view.bounds.size.width, view.bounds.size.height);
m_impl->onResizing(sz);
return frameSize;
}

- (void)windowDidEndLiveResize:(NSNotification*)notification
{
m_impl->onEndResizing();
Expand Down
73 changes: 0 additions & 73 deletions os/skia/resize_surface.cpp

This file was deleted.

37 changes: 0 additions & 37 deletions os/skia/resize_surface.h

This file was deleted.

21 changes: 5 additions & 16 deletions os/skia/skia_display.cpp
@@ -1,5 +1,5 @@
// LAF OS Library
// Copyright (c) 2018-2020 Igara Studio S.A.
// Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2012-2018 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -46,16 +46,18 @@ void SkiaDisplay::resetSkiaSurface()
m_surface = nullptr;

m_customSurface = false;
resize(m_window.clientSize());
resizeSkiaSurface(m_window.clientSize());
}

void SkiaDisplay::resize(const gfx::Size& size)
void SkiaDisplay::resizeSkiaSurface(const gfx::Size& size)
{
if (!m_initialized || m_customSurface)
return;

gfx::Size newSize(size.w / m_window.scale(),
size.h / m_window.scale());
newSize.w = std::max(1, newSize.w);
newSize.h = std::max(1, newSize.h);

if (m_initialized &&
m_surface &&
Expand Down Expand Up @@ -138,19 +140,6 @@ void SkiaDisplay::setFullscreen(bool state)
m_window.setFullscreen(state);
}

void SkiaDisplay::resetSurfaceAndQueueResizeDisplayEvent()
{
// Redraw the full skia surface
if (m_surface)
resetSkiaSurface();

// Generate the resizing display event to redraw everything.
Event ev;
ev.setType(Event::ResizeDisplay);
ev.setDisplay(this);
os::queue_event(ev);
}

void SkiaDisplay::setTitle(const std::string& title)
{
m_window.setTitle(title);
Expand Down
3 changes: 1 addition & 2 deletions os/skia/skia_display.h
Expand Up @@ -25,6 +25,7 @@ class SkiaDisplay : public Display {
bool isInitialized() const { return m_initialized; }
void setSkiaSurface(SkiaSurface* surface);
void resetSkiaSurface();
void resizeSkiaSurface(const gfx::Size& size);

void resize(const gfx::Size& size);

Expand Down Expand Up @@ -74,8 +75,6 @@ class SkiaDisplay : public Display {
void setColorSpace(const os::ColorSpaceRef& colorSpace);
os::ColorSpaceRef currentMonitorColorSpace() const;

void resetSurfaceAndQueueResizeDisplayEvent();

void onTabletAPIChange();

// Returns the HWND on Windows.
Expand Down

0 comments on commit 39ff912

Please sign in to comment.