Code Monkey home page Code Monkey logo

ahk.v1-2.neutron's Introduction

Welcome to Neutron for AutoHotkey v2

Neutron provides a powerful set of tools for build HTML-based user interfaces with AutoHotkey. It leverages the Trident engine, known for its use in Internet Explorer, because of its deep integration with the Microsoft Windows operating system and its wide availability across systems.

Notable features:

  • Create GUIs with HTML, CSS, JS, and AHK all working together.
  • Make responsive user interfaces that reflow when you resize the window, and scroll when elements go out of view.
  • Full customization of the title bar including fonts and colors.
  • Make better looking interfaces easily with web frameworks like Bootstrap.
  • Compile resources into your script and access them without extracting. Very useful for including images in the script!

Neutron Window

Listen and watch about Neutron on YouTube, from the May 2020 webinar recording. This was for a very early version of Neutron but it still contains many of the core concepts.

Getting Started with Neutron

The Neutron library is designed to be minimally invasive, easily included into existing scripts without major modifications. Neutron GUIs are created and managed similarly to native AutoHotkey GUIs.

; --- Creating a GUI ---
; Traditional syntax:
name := Gui(options, title)
; Neutron syntax:
name := NeutronWindow("html", "css", "js", "title")
; or
name := NeutronWindow("<!DOCTYPE html><html>full html document</html>")
; or
name := NeutronWindow().Load("file.html")

; --- Giving Window Options ---
; Traditional syntax:
name.Opt("+Option +Option")
; Neutron syntax:
name.Opt("+Option +Option")

; --- Showing the GUI ---
; Traditional syntax:
name.Show("w800 h600")
; Neutron syntax:
name.Show("w800 h600")

; --- Handling Events ---
; Traditional syntax:
name.OnEvent("Close", Handler)
name.OnEvent("Escape", Handler)
name.OnEvent("DropFiles", Handler)
Handler(gui, p*) {
	MsgBox "Events!"
}
; Neutron syntax:
name.OnEvent("Close", Handler)
	.OnEvent("Escape", Handler)
	.OnEvent("DropFiles", Handler)
MsgBox, Events!
Handler(gui, p*) {
	MsgBox "Events!"
}
return

; --- Hiding the GUI ---
; Traditional syntax:
name.Hide()
; Neutron syntax:
name.Hide()

; --- Destroying the GUI ---
; Traditional syntax:
name.Destroy()
; Neutron syntax:
name.Destroy()

Because all controls are now created through the HTML you provide to Neutron, you're going to need a new way to set up event handlers like the button gLabels you may be familiar with from native GUIs. Neutron provides the HTML/JS with a way to call functions defined in your AutoHotkey source. This is a very convenient method to set up event handlers.

The AHK function will receive any parameters passed by the JavaScript, with an extra "neutron" parameter passed in first that contains the Neutron instance that triggered the event.

neutron := NeutronWindow("<button onclick='ahk.Clicked(event)'>Hi</button>")
	.Show()
return

Clicked(neutron, event)
{
    MsgBox "You clicked: " event.target.innerText
}

Neutron offers a number of shorthands and utility methods to make it easier to interact with the page from AutoHotkey. A non-exhaustive list of these is below:

neutron := NeutronWindow("<span>a</span><span>b</span><span>c</span>")

; neutron.doc
; Equivalent to "document" in JS, used to access page contents
MsgBox, % neutron.doc.body.outerHTML

; neutron.wnd
; Equivalent to "window" in JS, used to access JS functions and variables
neutron.wnd.alert("Hi")

; neutron.qs("CSS Selector")
; Equivalent to "document.querySelector" in JS
element := neutron.qs(".main span")

; neutron.qsa("CSS Selector")
; Equivalent to "document.querySelectorAll" in JS
elements := neutron.qsa(".main span")

; NeutronWindow.Each(collection)
; Allow enumeration of JS arrays / element collections
for index, element in NeutronWindow.Each(elements)
    MsgBox, % index ": " element.innerText

; NeutronWindow.GetFormData(formElement)
; More easily processing of form data
formData := NeutronWindow.GetFormData(formElement)
MsgBox, % formData.fieldName ; Pull a single field
for name, value in formData ; Iterate all fields
    MsgBox, %name%: %value%

; Escape values to place into HTML
; NeutronWindow.EscapeHTML("unsafe text")
neutron.qs(".main").innerHTML := "<div>" NeutronWindow.EscapeHTML("a<'&>z") "</div>"
; NeutronWindow.FormatHTML("format string", "unsafe text 1", "unsafe text 2", ...)
neutron.qs(".main").innerHTML := NeutronWindow.FormatHTML("<div>{}</div>", "a<'&>z")

There's plenty more to be learned about Neutron from browsing its source and examples. Neutron's source is commented with great detail, and many methods have full runnable usage examples in the comments alongside them.

Neutron Examples

Neutron comes with a few example scripts to demonstrate how to use the library in a variety of situations. None of them are a one-size-fits-all solution, but they can be a great starting point for building your own Neutron UIs.

Template

Complexity: 1 / 5

This example is designed to show how to use the default Neutron template page. Because it uses the default template, it is also the simplest example to use and tweak as a beginner.

It is also designed to show how you would apply your own theming to the template without having to modify it directly, by applying CSS styling to the built-in template title bar elements.

Images

Complexity: 2 / 5

This example demonstrates displaying images through a tabbed multi-page interface. When compiled, this image gallery will stay contained in the exe file without having to extract the image resources to use them on the page.

Simple

Complexity: 2 / 5

This example, while named Simple, is not the most simple example. Instead, it is designed to demonstrate all of Neutron's built in behavior as a single custom page. It is meant to be simple by comparison to other examples like the Bootstrap example which demonstrate extending Neutron's functionality with third party web frameworks.

Bootstrap

Complexity: 4 / 5

This example is designed to show how you can use third party frameworks like Bootstrap to build advanced user interfaces, while still keeping all the code local. This script can be compiled and still function fine without the need to extract any files to a temporary directory.

As this example is more advanced, it assumes a stronger familiarity with the technology and may gloss over some parts more than other examples. If you're just getting started it may be helpful to work with some of the other example scripts first.

Compiling Neutron Scripts

Some Neutron scripts may require many dependencies, such as HTML, CSS, JS, SVG, and image files. Using one of the following methods, your Neutron script can be compiled into a portable exe that contains all these dependencies internally, without needing to extract them to a temporary directory for use.

A. Using compiler directives (requires AutoHotkey v1.1.33+)

  1. Add a ;@Ahk2Exe-AddResource *10 FileName directive for each dependent file.
    • For example: ;@Ahk2Exe-AddResource *10 index.html
    • Dependant files can be in any sub directory ;@Ahk2Exe-AddResource *10 %A_ScriptDir%\css\styles.css.
    • It doesn't matter where the directive line is in the script.
  2. Reference dependent files by name only, without any path portion.
    • In AHK: neutron.Load("index.html")
    • In HTML: <script src="index.js">, <link href="index.css" rel="stylesheet">, <img src="image.jpg">

B. Using FileInstall

  1. Any dependent files must be in the same directory as your AutoHotkey script file.
  2. Reference dependent files by name only, without any path portion.
    • In AHK: neutron.Load("index.html")
    • In HTML: <script src="index.js">, <link href="index.css" rel="stylesheet">, <img src="image.jpg">
  3. Have a FileInstall for each dependent file somewhere in the script. Put this somewhere that it won't be reached, such as just below the return after your auto-execute section.

When you do this, the dependent files will be saved into the EXE's RCDATA resources. Neutron's Load() method will detect when your script is compiled and read the dependencies from there instead of looking for files on the system.

Debugging Neutron

Along with traditional methods of debugging AutoHotkey scripts, you can access the IE developer tools by invoking the Microsoft utility IEChooser.exe. This tool, located at %SystemRoot%\System32\F12\IEChooser.exe, allows you to choose an IE frame embedded into any application and attach the regular web debugger to it.

This tool is part of the Windows 10 suite of debugging utilities introduced with Legacy Edge, and designed for debugging applications such as IE-based Office Add-ins. It also works pretty well for AHK scripts with embedded browser controls.

Copyright Disclaimer

The core components of this library have been released under the MIT license, but some examples contain third party code where other or additional license restrictions may apply.

Examples/Bootstrap/bootstrap.min.css

/*!
 * Bootstrap v4.5.0 (https://getbootstrap.com/)
 * Copyright 2011-2020 The Bootstrap Authors
 * Copyright 2011-2020 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */

Examples/Bootstrap/bootstrap.min.js

/*!
  * Bootstrap v4.3.1 (https://getbootstrap.com/)
  * Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  */

Examples/Bootstrap/jquery.min.js

/*! jQuery v3.5.1 | (c) JS Foundation and other contributors | jquery.org/license */

ahk.v1-2.neutron's People

Contributors

g33kdude avatar saifaqqad avatar mfaizsyahmi avatar samfisherirl avatar poa00 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.