{"id":1619,"date":"2018-04-24T18:35:00","date_gmt":"2018-04-24T18:35:00","guid":{"rendered":"https:\/\/xplicity.com\/?p=1619"},"modified":"2025-01-03T14:44:20","modified_gmt":"2025-01-03T11:44:20","slug":"part-2-creating-a-desktop-application-with-electron-angular-and-typescript-components-services-node-js-modules","status":"publish","type":"post","link":"https:\/\/xplicity.com\/part-2-creating-a-desktop-application-with-electron-angular-and-typescript-components-services-node-js-modules\/","title":{"rendered":"Part 2: Creating a desktop application with Electron, Angular and TypeScript: Components, Services, Node.js modules"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In the\u00a0<a href=\"https:\/\/xplicity.com\/part-1-creating-a-desktop-application-with-electron-angular-and-typescript-basic-setup\/\" data-type=\"post\" data-id=\"1627\" target=\"_blank\" rel=\"noreferrer noopener\">previous article<\/a>\u00a0we created and configured a basic Electron-Angular application. Now it\u2019s time to extend it and build a real application. In this article you will learn how to add Angular components and services to your desktop application, and access Node.js modules from the application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">IDEA \u2013 IMAGE GALLERY APPLICATION<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s build a simple image gallery app. On the left side of the window it should contain a directory explorer, which will allow you to browse your local file system\u2019s directories. On the top it will display the path of the selected directory. And, finally, on the right side it will display thumbnails of the selected directory\u2019s images.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"302\" src=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e380b25e.png\" alt=\"\" class=\"wp-image-1621\" srcset=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e380b25e.png 600w, https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e380b25e-300x151.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">INSTALL AND CONFIGURE ANGULAR MATERIAL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We don\u2019t want to spend too much time on styling and on creating nice looking components. Here is where Angular Material library comes to help us. The Angular Material library provides multiple components that comply with Material Design specifications. To install Angular Material and its dependencies, run the following command in the root directory of the project:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install --save @angular\/material @angular\/cdk @angular\/animations hammerjs<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add&nbsp;<strong>hammerjs&nbsp;<\/strong>import statement to the&nbsp;<strong>src\/main.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'hammerjs';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add&nbsp;<strong>BrowserAnimationsModule&nbsp;<\/strong>to the imports array in the&nbsp;<strong>app.module.ts<\/strong>&nbsp;(don\u2019t forget to add import statement):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { BrowserAnimationsModule } from '@angular\/platform-browser\/animations';\nimport { BrowserModule } from '@angular\/platform-browser';\nimport { NgModule } from '@angular\/core';\n\nimport { AppComponent } from '.\/app.component';\n\n\n@NgModule({\n  declarations: &#91;\n    AppComponent\n  ],\n  imports: &#91;\n    BrowserModule,\n    BrowserAnimationsModule\n  ],\n  providers: &#91;],\n  bootstrap: &#91;AppComponent]\n})\nexport class AppModule { }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Import Angular material theme. Add the following import statement to the&nbsp;<strong>src\/styles.css<\/strong>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@import \"~@angular\/material\/prebuilt-themes\/indigo-pink.css\";<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We will need to display some icons (e.g. folder icon). Include Material Icons font to the head section of the&nbsp;<strong>src\/index.html<\/strong>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;link href=\"https:\/\/fonts.googleapis.com\/icon?family=Material+Icons\" rel=\"stylesheet\"&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">CONFIGURE SASS SUPPORT<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Angular allows us to use Sass (Syntactically awesome style sheets) as style files. Sass extends regular CSS with variable declaration, style nesting, loops, and other cool stuff. We will use&nbsp;<strong>.scss<\/strong>&nbsp;syntax. To configure SASS support, do the following steps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Rename global styles file&nbsp;<strong>src\/styles.css<\/strong>&nbsp;to&nbsp;<strong>src\/styles.scss<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Modify style file extension in&nbsp;<strong>.angular-cli.json<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"styles\": &#91;\n  \"styles.scss\"\n],<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the same file change default style file extension from&nbsp;<strong>css&nbsp;<\/strong>to&nbsp;<strong>scss<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"defaults\": {\n  \"styleExt\": \"scss\",\n  \"component\": {}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Rename&nbsp;<strong>src\/app\/app.component.css<\/strong>&nbsp;file to&nbsp;<strong>src\/app\/app.component.scss<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Make sure that in&nbsp;<strong>src\/app\/app.component.ts<\/strong>&nbsp;file in the annotation of the component&nbsp;the extension is also changed to&nbsp;<strong>scss<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Component({\n  selector: 'app-root',\n  templateUrl: '.\/app.component.html',\n  styleUrls: &#91;'.\/app.component.scss']\n})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Some IDEs (e.g. WebStorm) do that automatically for you when you rename the file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">CREATE INITIAL LAYOUT<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The application\u2019s main layout will consist of three components: path component on the top, directory explorer on the left, and gallery component. Clear all existing contents of&nbsp;<strong>src\/app\/app.component.html<\/strong>&nbsp;and create placeholders for these components:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"path\"&gt;Placeholder for the path component&lt;\/div&gt;\n&lt;div class=\"main-wrapper\"&gt;\n  &lt;div class=\"directory-explorer\"&gt;Placeholder for the directory explorer component&lt;\/div&gt;\n  &lt;div class=\"gallery\"&gt;Placeholder for the gallery component&lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We will use Flexbox layout to align the components. Add styles to&nbsp;<strong>src\/app\/app.component.scss<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:host {\n  display: flex;\n  flex-direction: column;\n  height: 100%;\n}\n\n.path {\n  flex: 0;\n}\n\n.main-wrapper {\n  flex: 1;\n  display: flex;\n  flex-direction: row;\n\n  .directory-explorer {\n    flex: 0;\n  }\n\n  .gallery {\n    flex: 1;\n    overflow-y: auto;\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add global styles to&nbsp;<strong>src\/styles.scss<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>html, body {\n  height: 100%;\n  padding: 0;\n  margin: 0;\n  font-family: sans-serif;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Recompile the app to see the results.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run electron-build<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can use developer\u2019s console to check if components are properly aligned.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"430\" src=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e484da7f.png\" alt=\"\" class=\"wp-image-1622\" srcset=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e484da7f.png 600w, https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e484da7f-300x215.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">CONFIGURE ACCESS TO NODE.JS MODULES FROM ANGULAR<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Our desktop application consists of two applications \u2013 Electron application and Angular application. Electron application allows you to use Node.js modules which help to access file system, execute external processes, retrieve OS related information, and much more. Angular CLI doesn\u2019t properly compile an Angular app if you use Node.js directly from the app. But there is a workaround for solving this problem \u2013 create a separate JavaScript file where you import needed modules and include them to the build through&nbsp;<strong>.angular-cli.json<\/strong>&nbsp;file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>src\/js<\/strong>&nbsp;folder. In this folder create&nbsp;<strong>modules.js<\/strong>&nbsp;file and add the following contents to it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>window.NODE_FS = require('fs');\nwindow.NODE_PATH = require('path');\nwindow.NODE_OS = require('os');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see from the code we will use three modules&nbsp;<strong>fs<\/strong>,&nbsp;<strong>path<\/strong>, and&nbsp;<strong>os<\/strong>. The&nbsp;<strong>fs&nbsp;<\/strong>module provides methods for working the file system (e.g. read file). The&nbsp;<strong>path&nbsp;<\/strong>module contains utility methods for working with file paths (e.g. combining paths). The&nbsp;<strong>os&nbsp;<\/strong>module has operating system-related utilities (e.g. getting the home directory of the current user).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the&nbsp;<strong>js\/modules.js<\/strong>&nbsp;file to the global scripts array in the<strong>&nbsp;.angular-cli.json<\/strong>&nbsp;file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"scripts\": &#91;\n  \"js\/modules.js\"\n],<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now we need to access these modules from the Angular app. To do that, we will create an Angular service called&nbsp;<strong>RequiredModulesService<\/strong>. Run this command in the root of the application\u2019s root directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g service services\/required-modules<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Outside the generated class (<strong>ReqiredModulesService<\/strong>) in the&nbsp;<strong>src\/app\/services\/required-modules.service.ts,<\/strong>&nbsp;add function to access the global browser window object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function _window(): any {\n  \/\/ return the global browser window object\n  return window;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Inside the&nbsp;<strong>ReqiredModulesService<\/strong>&nbsp;class, declare&nbsp;<strong>browserWindow&nbsp;<\/strong>property, which will just call&nbsp;<strong>_window()<\/strong>&nbsp;function in its getter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>get browserWindow(): any {\n  return _window();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is a workaround for accessing&nbsp;<strong>window&nbsp;<\/strong>object from an Angular service.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And, finally, add wrapper properties for variables of our imported modules:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>get fs(): any {\n  return this.browserWindow.NODE_FS;\n}\n\nget os(): any {\n  return this.browserWindow.NODE_OS;\n}\n\nget path(): any {\n  return this.browserWindow.NODE_PATH;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The full contents of&nbsp;<strong>src\/app\/services\/required-modules.service.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Injectable } from '@angular\/core';\n\nfunction _window(): any {\n  \/\/ return the global browser window object\n  return window;\n}\n\n@Injectable()\nexport class RequiredModulesService {\n\n  constructor() { }\n\n  get browserWindow(): any {\n    return _window();\n  }\n\n  get fs(): any {\n    return this.browserWindow.NODE_FS;\n  }\n\n  get os(): any {\n    return this.browserWindow.NODE_OS;\n  }\n\n  get path(): any {\n    return this.browserWindow.NODE_PATH;\n  }\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the service to the&nbsp;<strong>providers&nbsp;<\/strong>array in the&nbsp;<strong>src\/app\/app.module.ts<\/strong>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>providers: &#91;\n  RequiredModulesService\n],<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Don\u2019t forget to add the import statement if your IDE didn\u2019t do that automatically:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { RequiredModulesService } from '.\/services\/required-modules.service';<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">CREATE FILEINFO MODEL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before we start creating other services and components, we need to define a model that will contain file\/directory-related information: full path, name, (is it directory or file?), and path in URL format (file:\/\/). Let\u2019s call it&nbsp;<strong>FileInfo<\/strong>. To create the model run the following command in the root directory of your project:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g class models\/file-info<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The contents of the&nbsp;<strong>src\/app\/models\/file-info.ts<\/strong>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export class FileInfo {\n  fullPath: string;\n  name: string;\n  isDirectory: boolean;\n  url: string;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">CREATE FILE SERVICE<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s create an Angular service that will be responsible for file system-related operations.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g service services\/file<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the service to the providers array in the&nbsp;<strong>src\/app\/app.module.ts<\/strong>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>providers: &#91;\n  RequiredModulesService,\n  FileService\n],<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the import statement if needed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { FileService } from '.\/services\/file.service';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Inject the&nbsp;<strong>RequiredModulesService<\/strong>&nbsp;to the just created&nbsp;<strong>src\/app\/services\/file.service.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>constructor(private requiredModulesService: RequiredModulesService) { }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Getting directory contents<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We need to write a method that will retrieve the contents of a directory. It should return array of&nbsp;<strong>FileInfo&nbsp;<\/strong>objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With the help of&nbsp;<strong>fs<\/strong>&nbsp;module\u2019s&nbsp;<strong>readdir(path, callback)<\/strong>&nbsp;method, we can get a list of the names of the files (and directories). To determine if an item is directory or file we should use&nbsp;<strong>fs.lstat(path, callback)<\/strong>&nbsp;method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you see, it is common for Node.js module\u2019s methods to return data using callbacks. But the proper Angular way is to use observables. Luckily RxJS has&nbsp;<strong>bindNodeCallback(nodeFunction)<\/strong>&nbsp;method for converting a Node.js-style callback to a function that returns observable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const readDirAsObservable = Observable.bindNodeCallback(\n  (path: string, callback: (error: Error, files: string&#91;]) =&gt; void) =&gt; this.requiredModulesService.fs.readdir(path, callback)\n);\n\nconst lstatAsObservable = Observable.bindNodeCallback(\n  (path: string, callback: (error: Error, stats: any) =&gt; void) =&gt; this.requiredModulesService.fs.lstat(path, callback)\n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also we want to merge results of&nbsp;<strong>fs.readdir()<\/strong>&nbsp;and&nbsp;<strong>fs.lstat(),<\/strong>&nbsp;and later on subscribe just to one Observable&nbsp;that will emit array of&nbsp;<strong>FileInfo&nbsp;<\/strong>objects. Since we already declared the observables for the&nbsp;<strong>fs<\/strong>&nbsp;module\u2019s functions it is easy now:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>readDirAsObservable(directoryPath)\n  .flatMap(f =&gt; f)\n  .mergeMap(f =&gt; lstatAsObservable(this.requiredModulesService.path.join(directoryPath, f)).catch(err =&gt; Observable.empty())\n    .map(s =&gt; {\n      const fileInfo = new FileInfo();\n      fileInfo.fullPath = this.requiredModulesService.path.join(directoryPath, f);\n      fileInfo.name = f;\n      fileInfo.isDirectory = s.isDirectory();\n      fileInfo.url = new URL(`file:\/\/\/${fileInfo.fullPath}`).href;\n      return fileInfo;\n    }));\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The complete code of the method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private getDirectoryContents(directoryPath: string): Observable {\n  const readDirAsObservable = Observable.bindNodeCallback(\n    (path: string, callback: (error: Error, files: string&#91;]) =&gt; void) =&gt; this.requiredModulesService.fs.readdir(path, callback)\n  );\n\n  const lstatAsObservable = Observable.bindNodeCallback(\n    (path: string, callback: (error: Error, stats: any) =&gt; void) =&gt; this.requiredModulesService.fs.lstat(path, callback)\n  );\n\n  return readDirAsObservable(directoryPath)\n    .flatMap(f =&gt; f)\n    .mergeMap(f =&gt; lstatAsObservable(this.requiredModulesService.path.join(directoryPath, f)).catch(err =&gt; Observable.empty())\n      .map(s =&gt; {\n        const fileInfo = new FileInfo();\n        fileInfo.fullPath = this.requiredModulesService.path.join(directoryPath, f);\n        fileInfo.name = f;\n        fileInfo.isDirectory = s.isDirectory();\n        fileInfo.url = new URL(`file:\/\/\/${fileInfo.fullPath}`).href;\n        return fileInfo;\n    }));\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can use this private method for public method&nbsp;that will return only directories&nbsp;and for other method that will return only files which can be filtered by extension.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>getDirectories(directoryPath: string): Observable {\n  return this.getDirectoryContents(directoryPath).filter(f =&gt; f.isDirectory).toArray();\n}\n\ngetFiles(directoryPath: string, extensions: string&#91;] = &#91;]): Observable {\n  return this.getDirectoryContents(directoryPath).filter(f =&gt; {\n    const extension = this.requiredModulesService.path.extname(f.name);\n      return extensions.length === 0 ?\n        f.isDirectory === false :\n        f.isDirectory === false &amp;&amp; (extensions.find(e =&gt; e === extension) != null);\n  }).toArray();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In our Directory Explorer component that we will create a bit later, we will want to navigate up from the opened directory by clicking \u201c..\u201d item. Let\u2019s write a method for getting parent directory info for a specified directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>getUpDirectory(directoryPath: string): FileInfo {\n  const upDirectory = this.requiredModulesService.path.join(directoryPath, '..');\n  const upDirInfo = new FileInfo();\n  upDirInfo.name = '..';\n  upDirInfo.fullPath = upDirectory;\n  upDirInfo.isDirectory = true;\n  return upDirInfo;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also we need a method for determining user\u2019s home directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>getHomeDirectoryPath(): string {\n  return this.requiredModulesService.os.homedir();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The full contents of&nbsp;<strong>src\/app\/services\/file.service.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Injectable } from '@angular\/core';\nimport { RequiredModulesService } from '.\/required-modules.service';\nimport { Observable } from 'rxjs\/Observable';\nimport { FileInfo } from '..\/models\/file-info';\n\n@Injectable()\nexport class FileService {\n\n  constructor(private requiredModulesService: RequiredModulesService) { }\n\n  private getDirectoryContents(directoryPath: string): Observable {\n    const readDirAsObservable = Observable.bindNodeCallback(\n      (path: string, callback: (error: Error, files: string&#91;]) =&gt; void) =&gt; this.requiredModulesService.fs.readdir(path, callback)\n    );\n\n    const lstatAsObservable = Observable.bindNodeCallback(\n      (path: string, callback: (error: Error, stats: any) =&gt; void) =&gt; this.requiredModulesService.fs.lstat(path, callback)\n    );\n\n    return readDirAsObservable(directoryPath)\n      .flatMap(f =&gt; f)\n      .mergeMap(f =&gt; lstatAsObservable(this.requiredModulesService.path.join(directoryPath, f)).catch(err =&gt; Observable.empty())\n        .map(s =&gt; {\n          const fileInfo = new FileInfo();\n          fileInfo.fullPath = this.requiredModulesService.path.join(directoryPath, f);\n          fileInfo.name = f;\n          fileInfo.isDirectory = s.isDirectory();\n          fileInfo.url = new URL(`file:\/\/\/${fileInfo.fullPath}`).href;\n          return fileInfo;\n      }));\n    }\n\n  getDirectories(directoryPath: string): Observable {\n    return this.getDirectoryContents(directoryPath).filter(f =&gt; f.isDirectory).toArray();\n  }\n\n  getFiles(directoryPath: string, extensions: string&#91;] = &#91;]): Observable {\n    return this.getDirectoryContents(directoryPath).filter(f =&gt; {\n      const extension = this.requiredModulesService.path.extname(f.name);\n        return extensions.length === 0 ?\n          f.isDirectory === false :\n          f.isDirectory === false &amp;&amp; (extensions.find(e =&gt; e === extension) != null);\n    }).toArray();\n  }\n\n  getUpDirectory(directoryPath: string): FileInfo {\n    const upDirectory = this.requiredModulesService.path.join(directoryPath, '..');\n    const upDirInfo = new FileInfo();\n    upDirInfo.name = '..';\n    upDirInfo.fullPath = upDirectory;\n    upDirInfo.isDirectory = true;\n    return upDirInfo;\n  }\n\n  getHomeDirectoryPath(): string {\n    return this.requiredModulesService.os.homedir();\n  }\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To make RxJS operators work, we need to add their import statements to the&nbsp;<strong>src\/app\/app.module.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'rxjs\/add\/operator\/mergeMap';\nimport 'rxjs\/add\/operator\/map';\nimport 'rxjs\/add\/observable\/bindNodeCallback';\nimport 'rxjs\/add\/operator\/toArray';\nimport 'rxjs\/add\/operator\/catch';\nimport 'rxjs\/add\/operator\/filter';\nimport 'rxjs\/add\/observable\/empty';<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">CREATE DIRECTORY EXPLORER COMPONENT<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Directory Explorer component will be responsible for navigating through directories. Run this command to create the Directory Explorer component:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g component components\/directory-explorer<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Component class<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Inject&nbsp;<strong>FileService<\/strong>&nbsp;and&nbsp;<strong>ChangeDetectorRef<\/strong>&nbsp;to the constructor of&nbsp;<strong>DirectoryExplorerComponent<\/strong>&nbsp;class (<strong>app\/components\/directory-explorer\/directory-explorer.component.ts<\/strong>):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>constructor(private fileService: FileService, private cd: ChangeDetectorRef) { }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We will use&nbsp;<strong>FileService<\/strong>&nbsp;for retrieving of directory contents.&nbsp;<strong>ChangeDetectorRef<\/strong>&nbsp;is needed for component updating workaround because Angular is unable to detect changes after calling Node.js module\u2019s methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Declare public&nbsp;<strong>directories<\/strong>&nbsp;array in the class. This array will be used to store the contents of a selected directory. It is needed for HTML template.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>directories: FileInfo&#91;];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Declare output property which will be used for notifying other components that selected path is changed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Output()\ncurrentPathChanged: EventEmitter = new EventEmitter();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add&nbsp;<strong>readDirectoryInfo<\/strong>&nbsp;method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>readDirectoryInfo(fileInfo: FileInfo) {\n  const upDirectory = this.fileService.getUpDirectory(fileInfo.fullPath);\n  this.fileService.getDirectories(fileInfo.fullPath).subscribe(f =&gt; {\n    const directories = &#91;];\n    if (upDirectory.fullPath !== fileInfo.fullPath) {\n      directories.push(upDirectory);\n    }\n    directories.push(...f);\n    this.directories = directories;\n    this.cd.detectChanges(); \/\/ workaround for refreshing the component\n    this.currentPathChanged.emit(fileInfo);\n  });\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It uses our previously created&nbsp;<strong>FileService<\/strong>&nbsp;to get the contents of a specified directory. It assigns the result to the&nbsp;<strong>directories<\/strong>&nbsp;array and adds parent directory item (\u201c..\u201d) to the beginning of the array. Then, the method calls change detector to update the current component, and finally, emits the selected directory (<strong>FileInfo&nbsp;<\/strong>object) through the&nbsp;<strong>currentPathChanged<\/strong>&nbsp;output.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On first component load (startup) we will set initial directory to the user\u2019s home directory and will call&nbsp;<strong>readDirectoryInfo<\/strong>&nbsp;to get its contents.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ngOnInit() {\n  const path = this.fileService.getHomeDirectoryPath();\n  const fileInfo = new FileInfo();\n  fileInfo.fullPath = path;\n  this.readDirectoryInfo(fileInfo);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We want to read a newly selected directory\u2019s contents when user selects directory in the Directory Explorer component. Add the following&nbsp;<strong>directoryClicked&nbsp;<\/strong>method that will be called whenever user clicked (selected) a new directory in the component.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>directoryClicked(fileInfo: FileInfo) {\n  this.readDirectoryInfo(fileInfo);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The full contents of&nbsp;<strong>app\/components\/directory-explorer\/directory-explorer.component.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Component, OnInit, ChangeDetectorRef, Output, EventEmitter } from '@angular\/core';\nimport { FileInfo } from '..\/..\/models\/file-info';\nimport { FileService } from '..\/..\/services\/file.service';\n\n@Component({\n  selector: 'app-directory-explorer',\n  templateUrl: '.\/directory-explorer.component.html',\n  styleUrls: &#91;'.\/directory-explorer.component.scss']\n})\nexport class DirectoryExplorerComponent implements OnInit {\n\n  @Output()\n  currentPathChanged: EventEmitter = new EventEmitter();\n\n  directories: FileInfo&#91;];\n\n  constructor(private fileService: FileService, private cd: ChangeDetectorRef) { }\n\n  ngOnInit() {\n    const path = this.fileService.getHomeDirectoryPath();\n    const fileInfo = new FileInfo();\n    fileInfo.fullPath = path;\n    this.readDirectoryInfo(fileInfo);\n  }\n\n  readDirectoryInfo(fileInfo: FileInfo) {\n    const upDirectory = this.fileService.getUpDirectory(fileInfo.fullPath);\n    this.fileService.getDirectories(fileInfo.fullPath).subscribe(f =&gt; {\n      const directories = &#91;];\n      if (upDirectory.fullPath !== fileInfo.fullPath) {\n        directories.push(upDirectory);\n      }\n      directories.push(...f);\n      this.directories = directories;\n      this.cd.detectChanges(); \/\/ workaround for refreshing the component\n      this.currentPathChanged.emit(fileInfo);\n    });\n  }\n\n  directoryClicked(fileInfo: FileInfo) {\n    this.readDirectoryInfo(fileInfo);\n  }\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">HTML template<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We will use&nbsp;<strong><\/strong>and&nbsp;<strong><\/strong>components from the Angular Material library to display list of directories. In order to use them we need to add&nbsp;<strong>MatListModule<\/strong>&nbsp;and&nbsp;<strong>MatIconModule<\/strong>&nbsp;to the&nbsp;<strong>imports<\/strong>&nbsp;array of the&nbsp;<strong>src\/app\/app.module.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>imports: &#91;\n  BrowserModule,\n  BrowserAnimationsModule,\n  MatListModule,\n  MatIconModule\n],<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check the import statement and add it if needed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {MatListModule, MatIconModule} from '@angular\/material';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following HTML template\u2019s code to the&nbsp;<strong>app\/components\/directory-explorer\/directory-explorer.component.html<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"container shadow\"&gt;\n  &lt;mat-list&gt;\n    &lt;mat-list-item *ngFor=\"let directory of directories\" (click)=\"directoryClicked(directory)\"&gt;\n      &lt;mat-icon mat-list-icon class=\"folder-icon\"&gt;folder&lt;\/mat-icon&gt;\n      &lt;span mat-line&gt;{{directory.name}}&lt;\/span&gt;\n    &lt;\/mat-list-item&gt;\n  &lt;\/mat-list&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here we display directories as list items with the help of&nbsp;<strong><\/strong>component and&nbsp;<strong>ngFor<\/strong>&nbsp;loop. Each list item has click event attached.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Styles<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following styles to the&nbsp;<strong>src\/app\/components\/directory-explorer\/directory-explorer.component.scss<\/strong>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.container {\n  overflow-y: auto;\n  height: 100%;\n  min-width: 230px;\n}\n\n.folder-icon {\n  color: #87cefa;\n}\n\nmat-list-item {\n  &amp;:hover {\n    background-color: transparentize(lightgrey, 0.8);\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add, finally, add shadow style and some small style adjustments for the mat-list-item to the global styles file (<strong>src\/styles.scss<\/strong>):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.shadow {\n  box-shadow: 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12);\n}\n\napp-directory-explorer {\n  mat-list-item {\n    font-size: 14px <strong>!important<\/strong>;\n    height: 20px <strong>!important<\/strong>;\n  }\n  .mat-list-item-content {\n    padding: 0 !important;\n\n    .mat-list-text {\n      padding-left: 5px <strong>!important<\/strong>;\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add the component to the App component<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we created the Directory Explorer component it\u2019s time to add it to the App component. In the&nbsp;<strong>src\/app\/app.component.html<\/strong>&nbsp;replace the placeholder text that we created earlier with&nbsp;<strong><\/strong>tags:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"path\"&gt;Placeholder for the path component&lt;\/div&gt;\n&lt;div class=\"main-wrapper\"&gt;\n  &lt;div class=\"directory-explorer\"&gt;&lt;app-directory-explorer&gt;&lt;\/app-directory-explorer&gt;&lt;\/div&gt;\n  &lt;div class=\"gallery\"&gt;Placeholder for the gallery component&lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Recompile the app and check if the Directory Explorer component works:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run electron-build<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"459\" src=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e6a2687a.png\" alt=\"\" class=\"wp-image-1623\" srcset=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e6a2687a.png 600w, https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e6a2687a-300x230.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">CREATE PATH COMPONENT<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Path component is very simple. Its purpose is to display the full path of the directory that we selected in the Directory Explorer component. To create the component run&nbsp;<strong>ng<\/strong>&nbsp;<strong>g&nbsp;<\/strong>component command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g component components\/path<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Component class<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Declare Angular input property in the&nbsp;<strong>src\/app\/components\/path\/path.component.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Input()\ncurrentPathInfo: FileInfo;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We will provide selected directory\u2019s&nbsp;<strong>FileInfo<\/strong>&nbsp;object to this properry from the App component later.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add&nbsp;<strong>currentPath<\/strong>&nbsp;string variable to the class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>currentPath: string;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This variable will be used for storing the active directory path in the component class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Implement&nbsp;<strong>OnChanges<\/strong>&nbsp;interface and add&nbsp;<strong>ngOnChanges<\/strong>&nbsp;method that just sets the&nbsp;<strong>currentPath<\/strong>&nbsp;variable\u2019s value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ngOnChanges(changes: SimpleChanges) {\n  if (changes.currentPathInfo.currentValue == null || changes.currentPathInfo.currentValue === '') {\n    this.currentPath = '';\n  } else {\n    this.currentPath = changes.currentPathInfo.currentValue.fullPath;\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method will be called every time when&nbsp;<strong>currentPathInfo<\/strong>&nbsp;input property will be changed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The full contents of the&nbsp;<strong>src\/app\/components\/path\/path.component.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Component, OnInit, Input, SimpleChanges, OnChanges } from '@angular\/core';\nimport { FileInfo } from '..\/..\/models\/file-info';\n\n@Component({\n  selector: 'app-path',\n  templateUrl: '.\/path.component.html',\n  styleUrls: &#91;'.\/path.component.scss']\n})\nexport class PathComponent implements OnInit, OnChanges {\n\n  @Input()\n  currentPathInfo: FileInfo;\n\n  currentPath: string;\n\n  constructor() { }\n\n  ngOnInit() {\n  }\n\n  ngOnChanges(changes: SimpleChanges) {\n    if (changes.currentPathInfo.currentValue == null || changes.currentPathInfo.currentValue === '') {\n      this.currentPath = '';\n    } else {\n      this.currentPath = changes.currentPathInfo.currentValue.fullPath;\n    }\n  }\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">HTML template<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the template (<strong>src\/app\/components\/path\/path.component.html<\/strong>) we just display&nbsp;<strong>currentPath<\/strong>&nbsp;variable which is wrapped to&nbsp;<strong>div<\/strong>&nbsp;for applying some styling:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"path shadow\"&gt;\n  {{currentPath}}\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Styles<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add styles for the component itself (<strong>:host<\/strong>) and&nbsp;<strong>path<\/strong>&nbsp;class to the&nbsp;<strong>src\/app\/components\/path\/path.component.scss<\/strong>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:host {\n  display: block;\n  background-color: white;\n  width: 100%;\n}\n\n.path {\n  margin: 0.5em;\n  padding: 0.5em;\n  background-color: #f0f8ff;\n  line-height: 0.75em;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add the component to the App component<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the App component\u2019s template (<strong>src\/app\/app.component.html<\/strong>) replace the placeholder text that we created for the path component with the real tag of the component (<strong>app-path<\/strong>).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"path\"&gt;\n  &lt;app-path&gt;&lt;\/app-path&gt;\n&lt;\/div&gt;\n&lt;div class=\"main-wrapper\"&gt;\n  &lt;div class=\"directory-explorer\"&gt;\n    &lt;app-directory-explorer&gt;&lt;\/app-directory-explorer&gt;\n  &lt;\/div&gt;\n  &lt;div class=\"gallery\"&gt;Placeholder for the gallery component&lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now we need to pass the selected directory from the Directory component to the Path component. We can do this wiring the Directory component\u2019s&nbsp;<strong>currentPathChanged<\/strong>&nbsp;output to some method in the AppComponent class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the&nbsp;<strong>src\/app\/app.component.ts<\/strong>&nbsp;declare&nbsp;<strong>currentPathInfo<\/strong>&nbsp;variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>currentPathInfo: FileInfo;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Inject&nbsp;<strong>ChangeDetectorRef<\/strong>&nbsp;to the constructor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>constructor(private changeDetector: ChangeDetectorRef) {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add&nbsp;<strong>currentPathChanged<\/strong>&nbsp;method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>currentPathChanged(fileInfo: FileInfo) {\n  this.currentPathInfo = fileInfo;\n  this.changeDetector.detectChanges();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method just sets&nbsp;<strong>currentPathInfo<\/strong>&nbsp;variable\u2019s value and calls the change detector.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The full contents of&nbsp;<strong>src\/app\/app.component.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Component, ChangeDetectorRef } from '@angular\/core';\nimport { FileInfo } from '.\/models\/file-info';\n\n@Component({\n  selector: 'app-root',\n  templateUrl: '.\/app.component.html',\n  styleUrls: &#91;'.\/app.component.scss']\n})\nexport class AppComponent {\n\n  currentPathInfo: FileInfo;\n\n  constructor(private changeDetector: ChangeDetectorRef) {}\n\n  currentPathChanged(fileInfo: FileInfo) {\n    this.currentPathInfo = fileInfo;\n    this.changeDetector.detectChanges();\n  }\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the app.component.html wire Directory component\u2019s&nbsp;<strong>currentPathChanged<\/strong>&nbsp;output to the&nbsp;<strong>currentPathChanged(fileInfo: FileInfo)<\/strong>&nbsp;method in the&nbsp;<strong>AppComponent<\/strong>&nbsp;class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;app-directory-explorer (currentPathChanged)=\"currentPathChanged($event)\"&gt;&lt;\/app-directory-explorer&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also wire the Path component\u2019s&nbsp;<strong>currentPathInfo<\/strong>&nbsp;input property to the App component\u2019s&nbsp;<strong>currentPathInfo<\/strong>&nbsp;variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;app-path &#91;currentPathInfo]=\"currentPathInfo\"&gt;&lt;\/app-path&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The full contents of&nbsp;<strong>src\/app\/app.component.html<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"path\"&gt;\n  &lt;app-path &#91;currentPathInfo]=\"currentPathInfo\"&gt;&lt;\/app-path&gt;\n&lt;\/div&gt;\n&lt;div class=\"main-wrapper\"&gt;\n  &lt;div class=\"directory-explorer\"&gt;\n    &lt;app-directory-explorer (currentPathChanged)=\"currentPathChanged($event)\"&gt;&lt;\/app-directory-explorer&gt;\n  &lt;\/div&gt;\n  &lt;div class=\"gallery\"&gt;Placeholder for the gallery component&lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Recompile the app and see the results:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run electron-build<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"459\" src=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e7d264d2.png\" alt=\"\" class=\"wp-image-1624\" srcset=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e7d264d2.png 600w, https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e7d264d2-300x230.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">CREATE GALLERY COMPONENT<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Gallery component will display thumbnails of the images of the directory that is selected in the Directory Explorer component. To create the component run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g component components\/gallery<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Component class<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Declare&nbsp;<strong>currentPath<\/strong>&nbsp;input property in the&nbsp;<strong>src\/app\/components\/gallery\/gallery.component.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Input()\ncurrentPath: string;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add&nbsp;<strong>supportedExtensions<\/strong>&nbsp;string array to store supported file extensions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>supportedExtensions: string&#91;] = &#91;'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.svg'];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also add&nbsp;<strong>imageFiles<\/strong>&nbsp;array which will store list of image files to display:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>imageFiles: FileInfo&#91;];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Inject the File service and&nbsp;<strong>ChangeDetectorRef<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>constructor(private fileService: FileService, private changeDetector: ChangeDetectorRef) { }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Implement&nbsp;<strong>OnChanges<\/strong>&nbsp;interface and add&nbsp;<strong>ngOnChanges<\/strong>&nbsp;method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ngOnChanges(changes: SimpleChanges): void {\n  if (changes.currentPath.currentValue == null || changes.currentPath.currentValue === '') {\n    this.imageFiles = &#91;];\n  } else {\n    const currentDirectory = changes.currentPath.currentValue;\n    this.fileService.getFiles(currentDirectory, this.supportedExtensions).subscribe(f =&gt; {\n      this.imageFiles = f;\n      this.changeDetector.detectChanges();\n    });\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method calls&nbsp;<strong>getFiles<\/strong>&nbsp;method from the&nbsp;<strong>FileService<\/strong>&nbsp;and assigns the result to the&nbsp;<strong>imageFiles<\/strong>&nbsp;array. The&nbsp;<strong>ngOnChanges<\/strong>&nbsp;method is called every time when the&nbsp;<strong>currentPath<\/strong>&nbsp;input property is changed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The full contents of the&nbsp;<strong>src\/app\/components\/gallery\/gallery.component.ts<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Component, OnInit, Input, OnChanges, SimpleChanges, ChangeDetectorRef } from '@angular\/core';\nimport { FileService } from '..\/..\/services\/file.service';\nimport { FileInfo } from '..\/..\/models\/file-info';\n\n@Component({\n  selector: 'app-gallery',\n  templateUrl: '.\/gallery.component.html',\n  styleUrls: &#91;'.\/gallery.component.scss']\n})\nexport class GalleryComponent implements OnInit, OnChanges {\n\n  @Input()\n  currentPath: string;\n\n  supportedExtensions: string&#91;] = &#91;'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.svg'];\n\n  imageFiles: FileInfo&#91;];\n\n  constructor(private fileService: FileService, private changeDetector: ChangeDetectorRef) { }\n\n  ngOnInit() {\n  }\n\n  ngOnChanges(changes: SimpleChanges): void {\n    if (changes.currentPath.currentValue == null || changes.currentPath.currentValue === '') {\n      this.imageFiles = &#91;];\n    } else {\n      const currentDirectory = changes.currentPath.currentValue;\n      this.fileService.getFiles(currentDirectory, this.supportedExtensions).subscribe(f =&gt; {\n        this.imageFiles = f;\n        this.changeDetector.detectChanges();\n      });\n    }\n  }\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">HTML template<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We will use&nbsp;<strong><\/strong>component from Angular Material library to display image thumbnails. Therefore, we need to add&nbsp;<strong>MatCardModule<\/strong>&nbsp;to the&nbsp;<strong>imports<\/strong>array in the&nbsp;<strong>src\/app\/app.module.ts<\/strong>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>imports: &#91;\n  BrowserModule,\n  BrowserAnimationsModule,\n  MatListModule,\n  MatIconModule,\n  MatCardModule\n],<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Don\u2019t forget to add the import statement for the&nbsp;<strong>MatCardModule<\/strong>&nbsp;if needed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {MatListModule, MatIconModule, MatCardModule} from '@angular\/material';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the HTML template we have&nbsp;<strong>ngFor<\/strong>&nbsp;loop through&nbsp;<strong>imageFiles<\/strong>&nbsp;array. The&nbsp;<strong><\/strong>represents image file info \u2013 file name as a title and thumbnail of the image. The contents of&nbsp;<strong>src\/app\/components\/gallery\/gallery.component.html<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"container\"&gt;\n  &lt;mat-card *ngFor=\"let file of imageFiles\" class=\"image-preview\"&gt;\n    &lt;mat-card-header&gt;\n      &lt;mat-card-title&gt;{{file.name}}&lt;\/mat-card-title&gt;\n    &lt;\/mat-card-header&gt;\n    &lt;img mat-card-image src=\"{{file.url}}\" alt=\"{{file.name}}\"&gt;\n  &lt;\/mat-card&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Styles<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add these styles to the&nbsp;<strong>src\/app\/components\/gallery\/gallery.component.scss<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:host {\n  height: 100%;\n  display: block;\n  padding: 0;\n}\n\n.container {\n  padding: 0.25em 0.5em;\n}\n\n.image-preview {\n  display: inline-block;\n  margin: 0.25em;\n\n  img {\n    max-width: 250px;\n    max-height: 250px;\n  }\n}\n\nmat-card-title {\n  font-weight: bold;\n  text-align: center;\n  width: 100%;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add the component to the App component<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the&nbsp;<strong>src\/app\/app.component.html<\/strong>&nbsp;replace the placeholder text for the gallery component with the Gallery component\u2019s tag and pass full path of the selected directory to it through the input property:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;app-gallery &#91;currentPath]=\"currentPathInfo?.fullPath\"&gt;&lt;\/app-gallery&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The full contents of&nbsp;<strong>src\/app\/app.component.html<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"path\"&gt;\n  &lt;app-path &#91;currentPathInfo]=\"currentPathInfo\"&gt;&lt;\/app-path&gt;\n&lt;\/div&gt;\n&lt;div class=\"main-wrapper\"&gt;\n  &lt;div class=\"directory-explorer\"&gt;\n    &lt;app-directory-explorer (currentPathChanged)=\"currentPathChanged($event)\"&gt;&lt;\/app-directory-explorer&gt;\n  &lt;\/div&gt;\n  &lt;div class=\"gallery\"&gt;\n    &lt;app-gallery &#91;currentPath]=\"currentPathInfo?.fullPath\"&gt;&lt;\/app-gallery&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Recompile the app and see the results.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run electron-build<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Navigate to a folder that contains some pictures and you should be able to see them in the Gallery component.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"448\" src=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e8c28be4.png\" alt=\"\" class=\"wp-image-1625\" srcset=\"https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e8c28be4.png 600w, https:\/\/xplicity.com\/wp-content\/uploads\/2023\/07\/img_5dac1e8c28be4-300x224.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">CONCLUSION<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We\u2019ve just created a simple Image Gallery desktop application. You\u2019ve learnt how to add services and components to the application and how to access Node.js modules from the Angular part of the Electron-Angular application. In the\u00a0<a href=\"https:\/\/xplicity.com\/part-3-creating-a-desktop-application-with-electron-angular-and-typescript-application-menu-system-tray-dialogs\/\" data-type=\"post\" data-id=\"1608\" target=\"_blank\" rel=\"noreferrer noopener\">next article<\/a>\u00a0we will extend our application and will learn how to configure application menu and system tray, and how to call system dialogs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The source code of this article\u2019s project is available&nbsp;<a href=\"https:\/\/gitlab.com\/atolvaisis\/electron-angular-pt2\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the\u00a0previous article\u00a0we created and configured a basic Electron-Angular application. Now it\u2019s time to extend it and build a real application. In this article you will learn how to add Angular components and services to your desktop application, and access Node.js modules from the application. IDEA \u2013 IMAGE GALLERY APPLICATION Let\u2019s build a simple image [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1620,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1619","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"acf":[],"_links":{"self":[{"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/posts\/1619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/comments?post=1619"}],"version-history":[{"count":2,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/posts\/1619\/revisions"}],"predecessor-version":[{"id":2502,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/posts\/1619\/revisions\/2502"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/media\/1620"}],"wp:attachment":[{"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/media?parent=1619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/categories?post=1619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xplicity.com\/wp-json\/wp\/v2\/tags?post=1619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}