webui-dev / webui

Use any web browser or WebView as GUI, with your preferred language in the backend and HTML5 in the frontend, all in a lightweight portable lib.
https://webui.me
MIT License
2.36k stars 144 forks source link

[Feature Request] Expose `_webui_find_best_browser` #325

Closed SpikeHD closed 1 month ago

SpikeHD commented 4 months ago

I feel as though there may be some conditional logic developers may want to do based on the browser to use, BEFORE the window is shown. An example of this would be conditionally loading a Firefox XPI or Chromium extension, depending on which browser will be used (assuming #307 implements the feature for both platforms).

I imagine it would look something like this:

Window myWindow = new_window();

if (webui_find_best_browser() == Firefox) {
  // Set XPI file in extensions to load
} else if (webui_find_best_browser() == ChromiumBased) {
  // Set ZIP or whatever Chromium uses I can't remember
} else {
  // Do nothing? Attempt to load both and see which works? Idk, up to the developer
}

// Since `show()` internally also calls `webui_find_best_browser()`, this should match what was determined earlier UNLESS there is already a `current_browser` set internally (see: https://github.com/webui-dev/webui/blob/2a5f0c98fbb267a5aed921a8678f3f108bb12a6e/src/webui.c#L5272)
myWindow.show("<html>Hello World!</html>")

Perhaps the function should also be renamed to something like webui_determine_browser or something, just to make it more clear.

jinzhongjia commented 4 months ago

Now, there is a api for choosing browser webui_show_browser: https://github.com/webui-dev/webui/blob/2a5f0c98fbb267a5aed921a8678f3f108bb12a6e/include/webui.h#L221

It will return whether the setting is successful, which can indeed achieve the effect you mentioned. But I think your proposal is better, it should provide an API to detect whether the browser is available

SpikeHD commented 4 months ago

Totally! I think that the API you linked covers 90% of cases, but it would be good to have something that allows devs to know before the browser launches, in case they need to do any specific preparations (preloading a browser specific library, showing a warning/message asking the user if they want to use the browser that was detected, etc.)

AlbertShown commented 4 months ago

Good point. We can probably create a new API like this:

WEBUI_EXPORT size_t webui_get_best_browser();
size_t webui_get_best_browser() {
    return _webui_find_best_browser();
}
AlbertShown commented 4 months ago
if (webui_get_best_browser() == Firefox) {

  // Set XPI file in extensions to load
  webui_show_browser(myWindow, HTML, Firefox); 

} else if (webui_get_best_browser() == Chrome) {

  // Set ZIP or whatever Chrome uses
  webui_show_browser(myWindow, HTML, Chrome); 

} else {

  // ...
  webui_show(myWindow, HTML); 

}