webui icon indicating copy to clipboard operation
webui copied to clipboard

[Feature Request] Expose `_webui_find_best_browser`

Open SpikeHD opened this issue 1 year ago • 4 comments

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.

SpikeHD avatar Feb 10 '24 23:02 SpikeHD

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

jinzhongjia avatar Feb 11 '24 15:02 jinzhongjia

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.)

SpikeHD avatar Feb 11 '24 20:02 SpikeHD

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 avatar Feb 12 '24 15:02 AlbertShown

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); 

}

AlbertShown avatar Feb 12 '24 16:02 AlbertShown