Application
Add Browser Controls to your Desktop App
Your desktop app is not a web browser
A web browser has buttons to control page navigation such as back, forward and reload. Your desktop app's navigation buttons are up to you.
You may decide that you don't need page navigation because the design of your app doesn't require it. Sometimes, however, it's useful to be able to have back and forward buttons in your desktop app.
This guide will show you how to add browser navigation to your desktop app.
Use your web apps codebase
A common question is “Where do I put the code for my desktop app”. The answer is that you put it in the same place that you put your web app code. You don't need to maintain a separate codebase.
Before you start you might want to read how to use ToDesktop's CSS hooks and Javascript hooks. You can also read about how to separate desktop logic from web app logic.
HTML
First up let's add some simple HTML (or JSX) for our app buttons. You can put this snippet wherever you wish your navigation buttons to appear.
Styles
Next up, we want to add some styles so that the buttons are hidden from our web app.
We can add some more styles to make our buttons look how we want them to look.
Making it work
Now let's make our buttons actually work. We can use ToDesktop's goBack() and goForward() methods to go back or forward through our navigation history.
Make buttons disabled when they are not navigable
Everything works exactly as it should 🎉. However, what if we are at the most recent position in history and we can't go forward? We should communicate that to our users and make the button disabled.
Listen for navigation changes
We use the "navigate"
event to make sure that the code below runs every-time that we navigate to a new page in our desktop app.
We then check if we can canGoForward()
and if we canGoBack()
. Based on the return boolean of these methods we make the button disabled or not.
Disabled button styles
Finally, let's add some more styles to our buttons so that they are faded when disabled.