{"id":13692,"date":"2024-02-27T10:00:00","date_gmt":"2024-02-27T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=13692"},"modified":"2024-02-27T10:46:11","modified_gmt":"2024-02-27T01:46:11","slug":"streamlining-electron-js-app-with-tray-icons","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/13692","title":{"rendered":"Streamlining Electron.js App with Tray Icons"},"content":{"rendered":"\n<p>In the realm of desktop application development, Electron.js stands out as a robust framework for building cross-platform apps using web technologies. Among its many features, integrating tray icons can significantly enhance user experience by providing quick access to essential functionalities. Today, let&#8217;s explore how we can implement tray icons in our Electron.js application, from setup to implementation.<\/p>\n\n\n\n<p><strong>Setting Up A Electron.js Project<\/strong><\/p>\n\n\n\n<p>First things first, let&#8217;s set up a basic Electron.js project:<\/p>\n\n\n\n<p>1. Initialize a new Node.js project<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir project_name &amp;&amp; cd project_name\nnpm init -y<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>2. Install Electron.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install electron<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>3. Create project files<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>project_name\/\n\u251c\u2500\u2500 icon.png\n\u251c\u2500\u2500 index.html\n\u251c\u2500\u2500 index.js\n\u251c\u2500\u2500 node_modules\n\u251c\u2500\u2500 package.json\n\u2514\u2500\u2500 package-lock.json<\/code><\/pre>\n\n\n\n<p>&#8211; `index.html` for the application&#8217;s UI.<\/p>\n\n\n\n<p>&#8211; `index.js` for Electron main process code.<\/p>\n\n\n\n<p>&#8211; An icon file (`icon.png`) for the tray icon.<\/p>\n\n\n\n<p>&#8211; add `start` command in package.json like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"name\": \"project_name\",\n  \"version\": \"1.0.0\",\n  \"description\": \"\",\n  \"main\": \"index.js\",\n  \"scripts\": {\n    \"test\": \"echo \\\"Error: no test specified\\\" &amp;&amp; exit 1\",\n    \"start\": \"electron .\"\n  },\n  \"keywords\": &#091;],\n  \"author\": \"\",\n  \"license\": \"ISC\",\n  \"dependencies\": {\n    \"electron\": \"^29.0.1\"\n  }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Implementing Tray Icon Functionality<\/strong><\/p>\n\n\n\n<p>Next, let&#8217;s dive into implementing the tray icon functionality in `index.js` file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const { app, Tray, BrowserWindow } = require('electron');\nconst path = require('path');\n\nlet tray = null;\nlet mainWindow = null;\n\napp.on('ready', () =&gt; {\n  tray = new Tray(path.join(__dirname, 'icon.png'));\n\n  mainWindow = new BrowserWindow({\n    width: 800,\n    height: 600,\n    show: false,\n    webPreferences: {\n      nodeIntegration: true\n    }\n  });\n\n  mainWindow.loadFile('index.html');\n\n  tray.on('click', () =&gt; {\n    mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();\n  });\n});<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Running Your Electron.js Application<\/strong><\/p>\n\n\n\n<p>To run Electron.js application, execute the following command in the terminal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm start<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>This command will start Electron application, and we should see the tray icon appear in our system tray. Clicking on the tray icon should toggle the visibility of the application window.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>Integrating tray icons into Electron.js application is a straightforward process that can greatly improve user experience. Whether it&#8217;s for displaying notifications, providing quick access to features, or simply adding a professional touch to our app, tray icons are a valuable addition to any desktop application.<\/p>\n\n\n\n<p>With Electron.js, implementing tray icons becomes seamless, allowing us to focus on crafting an exceptional user experience for our desktop application. So why not enhance our Electron.js app with a sleek tray icon for innovative ideas?<\/p>\n\n\n\n<p>Hope you enjoy that.<br><br>By Asahi<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13692\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/13692\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Streamlining Electron.js App with Tray Icons\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13692\" data-text=\"Streamlining Electron.js App with Tray Icons\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13692\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13692\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>In the realm of desktop application development, Electron.js stands out as a robust framework for building cro [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[72,77,90,100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/13692"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=13692"}],"version-history":[{"count":3,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/13692\/revisions"}],"predecessor-version":[{"id":13695,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/13692\/revisions\/13695"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=13692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=13692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=13692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}