Skip to content

Commit

Permalink
Remote: can transmit window size
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Apr 17, 2024
1 parent 9a41caa commit 0d7aa8a
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/hello_imgui/app_window_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ struct WindowGeometry

// Size of the application window
// used if fullScreenMode==NoFullScreen and sizeAuto==false. Default=(800, 600)
// The size will be handled as if it was specified for a 96PPI screen
// (i.e. a given size will correspond to the same physical size on different screens, whatever their DPI)
ScreenSize size = DefaultWindowSize;

// If sizeAuto=true, adapt the app window size to the presented widgets.
Expand Down
4 changes: 4 additions & 0 deletions src/hello_imgui/hello_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ ImGuiTestEngine* GetImGuiTestEngine();
// "Sdl - Vulkan"
std::string GetBackendDescription();

// `ChangeWindowSize(const ScreenSize &windowSize)`: sets the window size
// (useful if you want to change the window size during execution)
void ChangeWindowSize(const ScreenSize &windowSize);

// @@md


Expand Down
6 changes: 5 additions & 1 deletion src/hello_imgui/impl/hello_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,14 @@ ImGuiTestEngine* GetImGuiTestEngine() { return GHImGuiTestEngine; }
ImGuiTestEngine* GetImGuiTestEngine() { return nullptr; }
#endif

void ChangeWindowSize(const ScreenSize &windowSize)
{
gLastRunner->ChangeWindowSize(windowSize);
}


bool _isDisplayingOnRemoteServer()
{
return true;
#ifdef HELLOIMGUI_WITH_NETIMGUI
return HelloImGui::GetRunnerParams()->remoteParams.enableRemoting;
#else
Expand Down
50 changes: 45 additions & 5 deletions src/hello_imgui/internal/backend_impls/abstract_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,15 @@ class NetImGuiWrapper
}
}

void sendFonts()
{
_sendFonts_Impl();
}
void sendFonts()
{
_sendFonts_Impl();
}

bool isConnected()
{
return NetImgui::IsConnected();
}

private:
void InitiateConnection()
Expand All @@ -184,7 +189,7 @@ class NetImGuiWrapper
NetImgui::ConnectToApp(clientName().c_str(), remoteParams().serverHost.c_str(), remoteParams().serverPort);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
++ mNbConnectionsTentatives;
_sendFonts_Impl();
_sendFonts_Impl();
}

std::string clientName()
Expand Down Expand Up @@ -235,6 +240,17 @@ class NetImGuiWrapper
std::unique_ptr<NetImGuiWrapper> gNetImGuiWrapper;
bool gWaitingForRemoteDpiInfo = false;
#endif

bool _isConnectedToRemoteServer()
{
#ifdef HELLOIMGUI_WITH_NETIMGUI
if (!_isDisplayingOnRemoteServer())
return false;
return gNetImGuiWrapper->isConnected();
#else
return false;
#endif
}
// =====================================================================================================================
// </NetImGuiWrapper>
// =====================================================================================================================
Expand Down Expand Up @@ -304,6 +320,12 @@ bool AbstractRunner::WantAutoSize()
#endif
}

void AbstractRunner::ChangeWindowSize(HelloImGui::ScreenSize windowSize)
{
auto bounds = mBackendWindowHelper->GetWindowBounds(mWindow);
bounds.size = windowSize;
mBackendWindowHelper->SetWindowBounds(mWindow, bounds);
}

bool AbstractRunner::ShallSizeWindowRelativeTo96Ppi()
{
Expand Down Expand Up @@ -1149,6 +1171,24 @@ void AbstractRunner::CreateFramesAndRender()
mBackendWindowHelper->ShowWindow(mWindow);
}
}

// Transmit window size to remote server
#ifdef HELLOIMGUI_WITH_NETIMGUI
if (_isConnectedToRemoteServer() && (mIdxFrame > 3) && params.remoteParams.transmitWindowSize)
{
static bool wasSizeTransmitted = false;
if (!wasSizeTransmitted)
{
wasSizeTransmitted = true;
auto windowSize = params.appWindowParams.windowGeometry.size;
// When running on a remote server, we do not know the window DPI factor,
// so we treat the window size as if it was 96PPI
uint16_t width_96PPI = (uint16_t)windowSize[0];
uint16_t height_96PPI = (uint16_t)windowSize[1];
NetImgui::SetWindowSize_96PPI(width_96PPI, height_96PPI);
}
}
#endif
} // SCOPED_RELEASE_GIL_ON_MAIN_THREAD end

if(foldable_region) // Handle idling
Expand Down
1 change: 1 addition & 0 deletions src/hello_imgui/internal/backend_impls/abstract_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AbstractRunner
// For jupyter notebook, which displays a screenshot post execution
ImageBuffer ScreenshotRgb() { return mRenderingBackendCallbacks->Impl_ScreenshotRgb_3D(); }

void ChangeWindowSize(ScreenSize windowSize);

void LayoutSettings_SwitchLayout(const std::string& layoutName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace HelloImGui { namespace BackendApi
// It is only a class in order to enforce a consistent API between backends.
public:
WindowPointer CreateWindow(AppWindowParams &appWindowParams, const BackendOptions& backendOptions) override {
mWindowBounds.size = appWindowParams.windowGeometry.size;
return nullptr;
}

Expand All @@ -28,8 +29,12 @@ namespace HelloImGui { namespace BackendApi
bool IsWindowIconified(WindowPointer window) override { return false; }
void RaiseWindow(WindowPointer window) override {}

ScreenBounds GetWindowBounds(WindowPointer window) override { return {}; }
void SetWindowBounds(WindowPointer window, ScreenBounds windowBounds) override {}
ScreenBounds GetWindowBounds(WindowPointer window) override {
return mWindowBounds;
}
void SetWindowBounds(WindowPointer window, ScreenBounds windowBounds) override {
mWindowBounds = windowBounds;
}

void WaitForEventTimeout(double timeout_seconds) override {
std::this_thread::sleep_for(std::chrono::milliseconds((int)(timeout_seconds * 1000)));
Expand All @@ -41,6 +46,9 @@ namespace HelloImGui { namespace BackendApi
void ShowWindow(WindowPointer window) override {}
bool IsWindowHidden(WindowPointer window) override { return false; }

private:
ScreenBounds mWindowBounds = {};

};
}} // namespace HelloImGui { namespace BackendApi

Expand Down
5 changes: 5 additions & 0 deletions src/hello_imgui/runner_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ struct NetImGuiParams
// This is unused since it is the client (i.e. the app logic) that connects to the server,
// using NetImgui::ConnectToApp
uint32_t clientPort = 8889;

// If true, transmit the window size to the server
bool transmitWindowSize = false;
};

// @@md
Expand Down Expand Up @@ -350,6 +353,8 @@ struct SimpleRunnerParams

// `windowSize`: _ScreenSize, default={800, 600}_.
// Size of the window
// The size will be handled as if it was specified for a 96PPI screen
// (i.e. a given size will correspond to the same physical size on different screens, whatever their DPI)
ScreenSize windowSize = DefaultWindowSize;

// `fpsIdle`: _float, default=9_.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ int main(int, char**)
//runnerParams.rendererBackendType = HelloImGui::RendererBackendType::Vulkan;

runnerParams.remoteParams.enableRemoting = true;
runnerParams.remoteParams.transmitWindowSize = true;

HelloImGui::Run(runnerParams); // Note: with ImGuiBundle, it is also possible to use ImmApp::Run(...)

Expand Down
4 changes: 4 additions & 0 deletions src/netimgui_remote_display/netimgui_remote_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ int main(int argc, char **argv)
// Runner Params
HelloImGui::RunnerParams runnerParams;

NetImguiServer::UI::SetCallbackWindowSize96PPI([](uint16_t width, uint16_t height) {
HelloImGui::ChangeWindowSize({(int)width, (int)height});
});

runnerParams.callbacks.PostInit = [&pass_cmd_line_args_to_server]() {
pass_cmd_line_args_to_server();
NetImguiServer::UI::SetUseServerDPISettings(HelloImGui::DpiFontLoadingFactor());
Expand Down

0 comments on commit 0d7aa8a

Please sign in to comment.