html - 如何在 Electron 的自定义标题栏中显示/隐藏最大化/最小化按钮

标签 html electron angular9

到目前为止,我在 Electron 中创建了一个自定义标题栏(使用角度9),分别将这些按钮添加到html和click事件中,以最小化,最大化,恢复或关闭窗口。现在,当用户双击标题栏(由于css属性设置为-webkit-app-region: drag)或用户使用窗口捕捉功能最大化窗口时,就会出现问题。对于双击,我想到了在angular中使用dblclick事件,但仍然失败。那么,我该如何解决这个问题呢?

titlebar.component.html

<div class="titlebar" *ngIf="showTitleBar" (dblclick)="dblFunction()">
   
   <div class="navigation">
      <a class="normal-button material-icons" *ngIf="showBackButton">arrow_back</a>
      <div class="appTitle">{{title}}</div>
   </div>
   
   <div class="wincontrol">
      <a class="normal-button material-icons" (click)="minimize()">remove</a>
      <a class="normal-button material-icons" *ngIf="showMaxButton ; else showResButton" (click)="maximize()">crop_square</a>
      <ng-template #showResButton>
         <a class="normal-button material-icons" id="restore" (click)="restore()">flip_to_front</a>
      </ng-template>
      <a class="close-button material-icons" (click)="close()">clear</a>
   </div>
   
</div>


titlebar.component.ts

import { Component, OnInit } from '@angular/core';
import { WindowService } from 'src/app/services/window.service';
import { ElectronhelperService } from 'src/app/services/electronhelper.service';

@Component({
  selector: 'app-titlebar',
  templateUrl: './titlebar.component.html',
  styleUrls: ['./titlebar.component.scss']
})
export class TitlebarComponent implements OnInit {
  title = 'Electron-App' ;
  showMaxButton ;
  showTitleBar = true ;
  showBackButton = false ;

  constructor(private win: WindowService, private helper: ElectronhelperService){
    this.showMaxButton = !this.win.winSettings.wasMaximized ;
  }

  ngOnInit(): void {
  }

  minimize(){
    this.win.sendMinimize() ;
  }

  maximize(){
    this.showMaxButton = !this.showMaxButton ;
    this.win.sendMaximize() ;
  }

  restore(){
    this.showMaxButton = !this.showMaxButton ;
    this.win.sendRestore() ;
  }

  close(){
    this.win.sendClose() ;
  }

  dblFunction(){
    console.log('dbl clicked')
    this.showMaxButton = !this.showMaxButton ;
  }
}

最佳答案

因此,最终在浏览了整个Internet之后,由于区域运行在angular的代码之外而导致了区域问题。

titlebar.component.ts

import { Component, OnInit, NgZone } from '@angular/core';
import { WindowService } from 'src/app/services/window.service';
import { ElectronhelperService } from 'src/app/services/electronhelper.service';

@Component({
  selector: 'app-titlebar',
  templateUrl: './titlebar.component.html',
  styleUrls: ['./titlebar.component.scss']
})
export class TitlebarComponent implements OnInit {
  title = 'Electron-App' ;
  showMaxButton ;
  showTitleBar = true ;
  showBackButton = false ;
  constructor(private win: WindowService, private helper: ElectronhelperService, private zone: NgZone){
    this.showMaxButton = !this.win.winSettings.wasMaximized ;

    this.helper.win.on('maximize', this.toggleMaxButton.bind(this)) ;
    this.helper.win.on('unmaximize', this.toggleMaxButton.bind(this)) ;
  }

  ngOnInit(): void {
  }

  minimize(){
    this.helper.win.minimize() ;
  }

  maximize(){
    this.helper.win.maximize() ;
  }

  restore(){
    this.helper.win.unmaximize() ;
  }

  close(){
    this.helper.win.close() ;
  }

  toggleMaxButton(){
    this.zone.runTask(() => {
      if (this.helper.win.isMaximized()){
        this.showMaxButton = false ;
      }
      else{
        this.showMaxButton = true ;
      }
      console.log(this.showMaxButton)
    }) ;
  }
}


由于win.on事件是异步的,因此需要ngZone才能在angular内切换 bool 值。解决这个问题实际上花了一个星期!

关于html - 如何在 Electron 的自定义标题栏中显示/隐藏最大化/最小化按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62343961/

相关文章:

php - 在 sendgrid 电子邮件中的电子邮件内容中包含我的 html 代码

html - 看起来像图像按钮的链接不听颜色 : instruction in css

html - 如何使用 DNN 上的编辑器添加 HTML/CSS?如何在不依赖模块的情况下添加标记?

带触摸屏的 Electron JS kiosk 模式

angular - Karma-Jasmine:TypeError:无法读取未定义的属性(读取 'get')

Angular 9 服务的抽象类需要@injectable 装饰器吗?

html - 如何删除将鼠标悬停在图像上时出现的缩放图标?

javascript - 可拖动区域上的 Electron 上下文菜单

angular - Angular Electron -showOpenDialog typescript 错误?没有与 'OpenDialogOptions'类型相同的属性

Angular 9 i18n迁移-测试失败