-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hybrid apps? #1
Comments
Thank you for your attention. Embedding webview into same window is hard. So full-featured webview widget is like impossible. But you can look up vwebui if you can use webview inside another window. Also you can port your PWA application with Vebview.JS. But sad to say these solutions won't work inside Android. |
Thanks for your reply, I hope there will be some solution to this, and it could be a deal breaker to bring more popularity to v language. I have several projects (Web/Electron) but there is no way to get them to mobile platforms, having a strong backend and native support. I am monitoring tauri, looks like they are currently working on the mobile support. Why I am convinced to PWA over native UI:
The way how I am used to implement such applications:
Another simple example of the implementation: struct Article {
id int [primary; sql: serial]
title string
text string
}
struct IdModel {
id int [required]
}
struct ArticleModel {
id int
title string
text string
}
struct CreateArticleModel {
title string [required; maxlength: 50]
text string
}
struct UpdateArticleModel { ... }
struct PublishArticleModel { ... }
...
[api]
struct ArticleService {
db DbService // Injected service or repository
}
[api]
pub fn (article_service &ArticleService) get(query IdModel) ArticleModel {
result = sql article_service.db {
select from Article where id = $query.id
}
return result.map<ArticleModel>()
}
[api]
pub fn (article_service &ArticleService) create(form CreateArticleModel ) IdModel {
form.validate_or_throw() or { ValidationError };
result = sql article_service.db {
insert into Article (id, title, text) set ($form.id, $form.title, $form.text)
} or { DbError }
return IdModel { result }
} codegen generates the following js/ts (based on open api scheme, eg. https://github.com/Manweill/swagger-axios-codegen): export class IdModel {
'id'?: number;
constructor(data: undefined | any = {}) {
this['id'] = data['id'];
}
public static validationModel = { id: { required: true } };
}
.. all other models
export class ArticleService {
static get(params: { body?: IdModel; } = {} as any, options: IRequestOptions = {}): Promise<ArticleModel> {
return new Promise((resolve, reject) => {
let url = basePath + '/ArticleService/Get';
const configs: IRequestConfig = getConfigs('post', 'application/json', url, options);
let data = params.body;
configs.data = data;
if (web) {
axios(configs, resolve, reject);
} else if (mobile) {
js_interop(configs, resolve, reject);
} else {
throw Error("platform not supported");
}
});
}
... etc other services
} Example of js interop in .NET: https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/ I am aware of the following cons:
So what is actually required is a WebView, and the ability to communicate between the WebView and the backend (in this case mui). Nothing else. It is easy as that. |
I can try to create WebView window with webview/webview. This library has good cross-platform support, uses system-webviews but it requires C++11 minimum (and C++17 for Windows). If I can make working the library with V (without very complicated compile steps), I will add a WebView window into mui for only desktop platforms (for now). |
I got working webviews on Linux and Windows. That's not have API except C functions that provided by webview library for now, however i will write V-compatible functions (also that could be include functions not provided from C library like resize or move, but that is not first goal). You can follow updates from WebView branch. |
Cool. Thank you. 👍 As I can see there are two features required (if not already there) as a minimum to get hybrid app working:
I will test it this week. |
After latest commit at webview branch
You can load html files with
You can browse example code at webview branch. |
👍 I am going to test it now.
Every hybrid app has such ability. It is not like js can eval any v code in the backend. It is just some kind of a channel to get/post information from/to the backend, same as it was a http request. Eg.: in v: app_data.webview.register_handler('my_handler', handler) // registers a `my_handler` method in webview
fn handler(message string) ?string {
request := deserialize<ServiceRequest>(message)
mut result = ''
if request.route.starts_with('/Articles') {
srv := ArticleService{ db }
if request.route.starts_with('/Articles/Get') {
params := deserialize<GetArticleQuery>(request.body)
result = serialize<ArticleModel>(srv.get(params)?) or { error('could not execute service $data.route') }
} else {
error('could not find route $data.route')
}
} else {
error('could not find route $data.route')
}
return result
}
// somewhere in my code
struct ServiceRequest {
pub:
route string
body string
}
module articles
struct GetArticleQuery {
pub:
id int
}
struct ArticleModel {
pub:
id int
title string
text string
}
struct ArticleService {
db DatabaseService
}
pub fn (a ArticleService) get(q GetArticleQuery) ArticleModel {
return a.db.query<ArticleModel>(select * from articles where id = $q.id)
} and then js: <script>
function load_article(article_id) {
call_v("my_handler", "{ route: '/Articles/Get', body: { id: article_id } }") // returns Promise
.then(result => {
article_page.form = result.data;
}).catch(error => {
alert("Error: " + error);
});
}
</script> Same approach with You can look at this as an example: https://tauri.app/v1/guides/features/command |
👍
I guess the base path can be used. Or as an alternative some protocol |
I made a basic Articles example. This is not use any db or anything but it's good to explain V-JavaScript inter-op. Also this example written by vanilla JavaScript but could be used Vue.JS or another JavaScript library to make the application more powerful. You can browse the code. |
I tried to to run the webview example but I am getting an error "cannot import module "malisipi.mui.webview" (not found)" btw, before I have run: |
v installs default branch so you must install yourself, PS: Also if you use Windows, compiling example is could be too challenging. |
Maybe I have missed something, but I got the following error:
this is how I built the webview (copied from my terminal):
|
Looks like |
These what I have:
On executing the But these time that error disappeared. |
Your executables created correctly according V compiler logs, so should work fine and i can not recognize the bug.
|
Same with For
|
If a executable was created, can you upload?
I can reproduce the bug, i will fix it. |
Ok, I get the demo app working with the command However the Do we have any error logs somewhere? |
Sorry, i haven't created any log system.
Does this command create any executable? If yes, can you attach executable in your comment. |
Run without |
Also the issue is about like webview, can you check out you have microsoft webview2 runtime. If you not, you can download from Microsoft |
ok, the |
|
This example works (your compiled) on me. Did you copy WebView2Loader.dll to your app directory? You should copy |
Thanks, now everything is working. Would be nice to write a doc how to build the mui app. 👍 |
You're right. I will write a documentation before merge webview with main branch. |
I will write a vuejs example and see how it works by the end of this week. I can fork your branch, so it will come back as an example if you are happy with that. Later we can automate the build a little bit, so no hassle with installing and setting up the environment for build. |
Thanks for dealing with the repo. I will be grateful for your pull request. If you have a problem with project, you can ask here.
That is good idea, that could be hard to make compatible with all operating systems. However it is worth to make. |
So as far as I can see the custom uri scheme is required in order to serve local files. This will be the best and most secure approach accessing local asset files by the webview. I will keep working on it https://github.com/fairking/mui/tree/vuejs_app_example Also I asked WebView guys why it is not available out of the box: webview/webview#851 |
I understand why we need custom uri scheme really. I will watch webview/webview#851 for now. If their response is positive, i will wait their implementation this feature. Else, i will patch webview again to get that. Also, your example looks good especially for peoples wants use web & v together. |
Looks to be a great project. Hope that it does not fall off from being sidetracked or distracted with webview or porting of PWA, and can maintain a balance. Not everyone is coming from or wants to go in that direction. |
As a senior backend developer I can understand you. But it depends what are you going to build. Simple apps, tools, utils are best with native UI, no doubt. However more complex and constantly changing frontend requires html/js. Just simple example. As a subscription manager and join membership app (web and android) we have to build a new unique app for every new customer (1 month of work). Please note, the API (backend) had minimal or no changes. If we go with the native approach, it will take at least 3 months instead. The second issue, we didn't have backend developers too much in which case native UI requires. But if it comes to scan barcode, NFC, camera, files and many other features the webview lack of, the native tools are very useful as well in such situations. |
First, let us acknowledge that it's fantastic what Malisipi has done, and that his project can accommodate both. It is of course a matter of perspective, as to which people may prefer, native UI or HTML/JS. There are those that prefer native UIs too. It's not just one over the other. I'm just pointing out that originally it appears the project was not about webview or the porting of PWA, but native UIs and has various related items on the to-do list. Luckily, Malisipi was so far along that he could do both, and its great that it has gone that way. |
Yes, I agree with what is going on here and the direction MUI is generally taking. Much appreciated @malisipi ! I came here only to emphasize that the most successful apps from small dev teams use a hybrid strategy - they build part of the GUI (yes, not just backend!) in native technology (here either Android Java or V bindings to the Android Java API) and the other part from one (or multiple!) webviews. Note the native GUI parts can arbitrarily overlap with webviews (with transparency both ways, events coming through both ways, and other goodies) and thus one can construct a really natively-feeling apps (relatively low latency, theme-aware, platform-specific gesture recognition, ...) while maintaining the rapid development pace. |
This is awesome. Is there any plans to implement some kind of WebView, so we can port web SPA/PWA applications, the same way how tauri is doing? I am particularly interesting in Android. And of course I can rewrite my backend in vlang. ;-)
The text was updated successfully, but these errors were encountered: