Skip to content

Commit bae8cd5

Browse files
committed
Improve webview integration
Signed-off-by: falkTX <[email protected]>
1 parent 5eac063 commit bae8cd5

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

plugins/WebBrowser/PluginUI.cpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ START_NAMESPACE_DISTRHO
1616

1717
class WebBrowserUI : public UI
1818
{
19-
bool urlChanged = true;
19+
bool urlChanged = false;
20+
bool urlFirstInit = true;
2021
bool urlNeedsReload = false;
2122
char urlLabelBuffer[0xff] = "https://github.com/DISTRHO/dear-plugins";
2223
uint urlLabelHeight = 0;
@@ -45,6 +46,8 @@ class WebBrowserUI : public UI
4546
setGeometryConstraints(width, height);
4647
setSize(width, height);
4748
}
49+
50+
setFontSize(16);
4851
}
4952

5053
protected:
@@ -74,6 +77,8 @@ class WebBrowserUI : public UI
7477

7578
fprintf(stderr, "url changed to %s\n", value);
7679

80+
const double scaleFactor = getScaleFactor();
81+
7782
WebViewOptions opts;
7883
opts.offset.y = urlLabelHeight;
7984

@@ -82,7 +87,7 @@ class WebBrowserUI : public UI
8287
getWindow().getNativeWindowHandle(),
8388
getWidth(),
8489
getHeight() - urlLabelHeight,
85-
getScaleFactor(),
90+
scaleFactor,
8691
opts));
8792
}
8893

@@ -117,11 +122,23 @@ class WebBrowserUI : public UI
117122
ImGui::TextUnformatted("URL:");
118123
ImGui::SameLine();
119124

120-
ImGui::SetNextItemWidth(-70);
121-
urlChanged |= ImGui::InputText("", urlLabelBuffer, sizeof(urlLabelBuffer), textFlags);
125+
ImGui::SetNextItemWidth(-76 * getScaleFactor());
126+
if (ImGui::InputText("", urlLabelBuffer, sizeof(urlLabelBuffer) - 8, textFlags))
127+
{
128+
urlChanged = true;
129+
130+
// add http:// suffix is no protocol is defined in newly changed URL
131+
if (std::strchr(urlLabelBuffer, ':') == nullptr)
132+
{
133+
std::memmove(urlLabelBuffer + 7, urlLabelBuffer, std::strlen(urlLabelBuffer) + 1);
134+
urlLabelBuffer[sizeof(urlLabelBuffer) - 1] = '\0';
135+
std::memcpy(urlLabelBuffer, "http://", 7);
136+
}
137+
}
122138
ImGui::SameLine();
123139

124-
urlNeedsReload |= ImGui::Button("Reload");
140+
if (ImGui::Button("Reload"))
141+
urlNeedsReload = true;
125142

126143
ImGui::EndGroup();
127144
}
@@ -139,16 +156,20 @@ class WebBrowserUI : public UI
139156
*/
140157
void uiIdle() override
141158
{
142-
if (urlChanged)
159+
if (urlChanged || urlFirstInit)
143160
{
144-
urlChanged = false;
145-
urlNeedsReload = false;
146-
147161
// change our webview contents
148162
stateChanged("url", urlLabelBuffer);
149163

150-
// inform host of the state change
151-
setState("url", urlLabelBuffer);
164+
if (urlFirstInit)
165+
{
166+
// inform host of the state change
167+
setState("url", urlLabelBuffer);
168+
urlFirstInit = false;
169+
}
170+
171+
urlChanged = false;
172+
urlNeedsReload = false;
152173
}
153174

154175
if (urlNeedsReload)
@@ -174,7 +195,7 @@ class WebBrowserUI : public UI
174195
UI::onResize(ev);
175196

176197
if (const WebViewHandle handle = webview.get())
177-
webViewResize(handle, ev.size.getWidth(), ev.size.getHeight(), getScaleFactor());
198+
webViewResize(handle, ev.size.getWidth(), ev.size.getHeight() - urlLabelHeight, getScaleFactor());
178199
}
179200

180201
// ----------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)