顯示具有 atom 標籤的文章。 顯示所有文章
顯示具有 atom 標籤的文章。 顯示所有文章

2016年12月29日 星期四

electron 上手筆記

electron 上手筆記

這裡 electron 是說 github 推的跨平台桌面應用程式開發工具,使用 html, css, javascript 開發。

文件、範例哪兒找?

有無簡易模板?

請參閱 quick-start 那份文件,我這裡也略述之。

專案目錄檔案結構

專案名稱/
– package.json
– main.js
– index.html

package.json

package.json 的內容如下:

{
    "name":"專案名稱",
    "version":"0.1.0",
    "main":"main.js"
}

如果沒有 main 這欄,electron 會去找 index.js 來用。

main.js

main.js 的工作是建立 window 以及處理系統事件。(這裡的系統事件指的是 electron 裡的 app 物件事件)

以下是範例:

const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({width: 800, height: 600})

  // and load the index.html of the app.
  win.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))

  // Open the DevTools.
  win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
    createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

index.html

index.html 就等於是畫面定義。範例如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using node <script>document.write(process.versions.node)</script>,
    Chrome <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>
</html>

開發時如何執行程式?

electron 安裝在 global

在專案目錄裡的命令列執行

electron .

electron 安裝在 local

在專案目錄裡的命令列執行

macOS/Linux

$ ./node_modules/.bin/electron .

Windows

$ .\node_modules\.bin\electron .

electron 是來自 binary download

Windows

$ .\electron\electron.exe your-app\

Linux

$ ./electron/electron your-app/

macOS

$ ./Electron.app/Contents/MacOS/Electron your-app/

有沒有現成的範例檔下載?

用 npm 來做:

# Clone the repository
$ git clone https://github.com/electron/electron-quick-start
# Go into the repository
$ cd electron-quick-start
# Install dependencies
$ npm install
# Run the app
$ npm start

2016年9月23日 星期五

[atom]開啟多個視窗

前言

在使用上,如果已經開啟一個 atom 視窗。之後若是再開啟其他位置的檔案,會通通擠在一起視窗裡,在 tree view 也都放在同一個 project folder 裡。在我自己使用上,因為會在不同的位置開一些檔案做參考,不屬於同一個專案,全部擠在一起不方便。有時檔名一樣,很容易搞錯。

對手怎麼做?

我把vscode 視為 atom 的對手,它的行為就是不同目錄開起來就是會不同視窗。搞錯機會很低。

解決方法

網路上是有說用命令列參數 -n--new-window,例如 atom -n ~/file。但是在 windows 下,其實習慣的是右鍵來處理。可惜沒找到。

另一個方法則是開啟之後,對不同目錄的檔案的頁籤,按右鍵,會出現「open in new window」,按下去就會再開一個視窗。只是它會把原先那個目錄留在 tree view 裡,這樣就是兩個 project folder。像我比較龜毛就會去把它移掉。對著目錄按右鍵,選擇「remove project folder」這樣就比較乾淨一點。

更新: 我找到更簡單的方法。如果在其他目錄也要開檔案,不要直接開啟那個檔案,而是開啟那個目錄,這樣就會再開一個 atom 視窗。這就正合我意。‧‧‧‧‧

2016年9月12日 星期一

[atom]設定 python 開發環境

前言

要知道好不好用,就得用心體會,就拿來試試開發 python 程式吧。

材料

谷歌到一系列 youtube ,就照本宣科試試。

https://www.youtube.com/watch?v=uve1tjVIQ6c

步驟:格式設定

  1. 顯示縮排的尺 (Show Indent Guide)。 在 1.10.2 的版本是放在 Settings > Editor 這裡。
  2. 使用 Soft Tabs
  3. Tabs length 設成 4

步驟:套件安裝

  1. 啟用 autosave。對 Settings 也要啟用。
  2. 安裝 atom-runner。這個可執行 python。如果你是 apple 系統,要在 config 加東西,請參照影片。

步驟:執行

  1. 寫一段程式

    import random
    
    for i in range(5):
        print(random.random())
    
  2. 執行請按(我是用 windows) alt-r

還有 autocomplete 與 debugger

autocomplete 與 refactor

照那個 youtube 設定只有能執行 python。但是沒有加裝 autocomplete-python 這個套件的話,基本上它所提示的只是你已經輸入過的關鍵字,當然不夠用。「跳到定義去 go-to-definition 」這個 IDLE 沒有的夢幻功能就可以用 ctrl-alt-g 來使用。因為一次按三個按鍵太糟,請安裝 hyperclick 套件,這樣就可以按住 ctrl 用滑鼠去點你想找定義的關鍵字。

https://atom.io/packages/autocomplete-python

為了寫程式 refactor 方便,Python Tools 加進來是最好的。

https://atom.io/packages/python-tools

debugger

debugger 目前有兩個套件,我選擇 https://atom.io/packages/python-debugger 。我不選 https://atom.io/packages/atom-python-debugger 的原因是不想多改 code 插 trace。

python-debugger 它使用的 alt-r 與 atom-runner 重覆,所以必須要使用選單 Packages > python-debugger > Toggle 來開啟執行視窗。這是要注意的地方。如果直接按 alt-r 會直接執行 python code。

[atom]編輯器體驗

起因

之前網路上看到有人把 sublime 跟 atom 比,才知道 github 出了一個 atom editor。但還沒有想要用看看。但是後來遇到朋友已開始用了,所以也來試試看,主要想要看看它能否當做 python IDE 與 markdown 編輯器。

背景

因為我會常常在 windows, linux, mac 環境跑來跑去。所以會需要一個通用的編輯器。我現在是在 windows 裡裝 notepad++,在 linux 用 pico/vi/gedit,在 mac 則是用 python IDLE 來暫用。目前來說,我在 mac 開文章的機會非常少,多是開來寫 python 所以就是用 python IDLE 就好了。原本有考慮 sublime,但是要讓我碰到的電腦都要裝 sublime 的話又好像太過份。而且 sublime 在討論上、套件上又好像比較少,也許是因為它屬於專有軟體的關係。所以,先試試 atom。他的用戶多,套件也會比較多一點。將來若是有自己開發套件,要連同主體一起散佈的話,也比較不會被說話。當然,應該也可以開發 sublime 套件,要賣給別人的時候請客戶再多付 sublime 的錢。這個方面我要再多些時間考慮。

擴充性

以擴充性來說,我當然還是喜歡 sublime 的 python api。atom 是用 node.js,對我來說還是不如 python 習慣。尤其 javascript 的非同步特性是不是合適套件開發,這不試過真不知道。

markdown 支援

最近喜歡使用 markdown 來寫文章,atom 上有官方內建的 markdown preview,也有社群加強版的 markdown preview plus。而 sublime 就要找看看。這也就是我會選擇多數人用的原因,這樣比較容易 google 到套件及使用方法。少人用的就會難找及難學。從 markdown 的角度來看,現在是有免費線上的 StackEdit 可用,但是若要固定下來,的確還是選執行檔編輯器會比較好。

ps. 其實 sublime 的 markdown preview 用 google 一下就出來了。https://www.youtube.com/watch?v=XnPkdcBGBlw 。但是它的 preview 不是即時,也不是在 sublime 裡,而是讓你指定轉成 html ,再用瀏覽器開啟。(也可以換成其他格式再用其他程式開啟。)這樣就不如 atom 的好了。

小發現

現在使用上有幾個感覺:
* 啟動比 notepad++ 慢很多,跟 jedit 有得拼了(比慢)。
* 一開始中文字型怪怪的。也許用到大陸字體,「置」這個字裡面是一豎而不是兩個橫。
於是按照以下連結,改成 Noto Sans CJK TC。
http://lsymind.blogspot.tw/2015/06/atom_22.html

官方說明文件

https://atom.io/docs