you need to build a RUST desktop app!!

Travis Media
3 Jan 202427:20

Summary

TLDRTravis from Travis., media shares a tutorial on building a simple desktop app called 'Income Divider' using Rust and the Slint UI framework. The app facilitates the division of income into four parts following the profit-first methodology, streamlining a process Travis found tedious with calculators and paper. The tutorial covers setting up the project with cargo generate, designing the UI with Slint, and implementing business logic in Rust. It also touches on advanced features like animations and creating a macOS app bundle, providing a practical project to learn Rust and desktop app development.

Takeaways

  • 😀 Travis is creating a simple desktop app called 'Income Divider' to practice Rust and to streamline his business income division process.
  • 🛠 The app is built using the Slint UI framework, which allows for a user interface with the flexibility to implement business logic in Rust, C++, or Node.js.
  • 📈 Travis follows the 'Profit First' methodology, dividing income into four parts: taxes, owner pay, profit, and operating expenses.
  • 📝 The app replaces the manual process of using a calculator and writing on paper, aiming for a more efficient and organized approach.
  • 💻 Travis chose Slint for its wide range of use cases, including embedded apps, desktop apps, and industrial applications, and its adoption by companies like SK Signate.
  • 🔧 The tutorial involves setting up a Rust project using cargo generate and cargo bundle for creating a release-ready binary.
  • 📚 Travis emphasizes the importance of learning concepts over languages and suggests focusing on Slint documentation for quick understanding.
  • 🎨 The app's UI is designed with a grid layout, input for income amount, a calculate button, and a results display area.
  • 🔢 Business logic in Rust handles the calculations based on predefined percentages for taxes, owner pay, profit, and operating expenses.
  • 📝 The script provides a step-by-step guide on setting up the Slint environment, coding the UI, and integrating it with Rust for functionality.
  • 🎉 The final app includes an animation feature that reveals the results in a curtain-like motion when the calculate button is pressed.

Q & A

  • What is the main purpose of the 'income divider' desktop app created by Travis?

    -The 'income divider' desktop app is designed to follow the profit first methodology, dividing income into four parts for taxes, owner's pay, profit, and operating expenses, which Travis finds more practical and less tedious than using a calculator and writing on paper.

  • Why did Travis choose to use Rust for building the 'income divider' app?

    -Travis chose Rust because he aims to learn it better and it allows him to practice building desktop apps. Rust also provides the freedom to perform business logic while using the Slint UI framework for the user interface.

  • What is Slint and how does it relate to the 'income divider' app?

    -Slint is a UI framework that supplies the user interface for apps, with the flexibility to perform business logic using various programming languages. Travis used Slint with Rust to create the 'income divider' app.

  • What are some of the use cases for Slint as mentioned in the script?

    -Slint is used in various applications such as embedded apps, desktop apps, industrial, consumer, automotive, and medical interfaces. It is also used for building instrument clusters for motorcycles and two-wheelers, and by companies like SK Signate for electric vehicle charging solutions.

  • How does the 'profit first methodology' mentioned in the script influence the design of the 'income divider' app?

    -The 'profit first methodology' influenced the design by requiring the app to divide the income into specific categories: taxes, owner's pay, profit, and operating expenses, which is the core functionality of the 'income divider' app.

  • What is the significance of the 'cargo generate' tool in creating the new Rust project for the app?

    -The 'cargo generate' tool is used to scaffold a new Rust project with a specified template. It simplifies the project setup process and is part of the first steps in creating the 'income divider' app.

  • How does the Slint documentation guide the development of the 'income divider' app's UI?

    -The Slint documentation provides a comprehensive guide on concepts, syntax, built-in elements, widgets, and examples. It helps Travis understand how to structure the UI, position elements, and use properties and callbacks for the app's functionality.

  • What is the role of the 'main.rs' file in the Rust project for the 'income divider' app?

    -The 'main.rs' file contains the business logic of the app. It is where the calculations for dividing the income are performed and where the app interacts with the Slint UI components through callbacks and property setters.

  • How does the 'cargo bundle' command contribute to the finalization of the 'income divider' app?

    -The 'cargo bundle' command is used to create a distributable version of the app, making it easy to install and run without needing to locate the binary file each time.

  • What additional feature did Travis implement in the 'income divider' app for displaying results?

    -Travis implemented an animation feature where clicking the 'calculate' button causes a curtain-like opening of two rectangles to reveal the calculated results.

Outlines

00:00

🛠️ Building a Practical Rust Desktop App with Slint

Travis from travis.media introduces a project to create a simple desktop application named 'Income Divider' using Rust and the Slint UI framework. The app is designed to facilitate the division of income into four parts following the profit-first methodology, which Travis finds more practical and less tedious than manual calculations. He chose Slint for its flexibility and its use in various industries, including automotive and medical solutions. Travis emphasizes the importance of learning Rust for building desktop apps and provides a step-by-step guide on setting up the project using cargo generate and modifying the UI with Slint.

05:00

📚 Learning Slint UI Framework and Building the Frontend

The tutorial continues with an in-depth exploration of the Slint UI framework, focusing on its syntax, built-in elements, and widgets. Travis demonstrates how to create a user interface with a grid layout, text elements, input fields, and buttons. He also discusses the importance of understanding the concepts behind UI frameworks rather than just learning multiple languages. The process includes setting properties for elements, using the live preview feature in VS Code, and compiling the app to ensure the frontend design works as intended.

10:02

🔧 Integrating Rust for Business Logic and Callbacks

Travis explains the transition from the frontend UI to the backend business logic, which is handled in Rust. He details the process of setting up properties and callbacks within the Slint file, such as 'on divide income', which will be used to perform calculations when the calculate button is clicked. The tutorial covers the Rust API, explaining how to use setters and getters for top-level properties and how to invoke callbacks. The goal is to connect the UI elements with the Rust logic to make the app functional.

15:06

📊 Implementing Income Division Calculations in Rust

The video script describes the implementation of the income division logic in Rust. Travis outlines the tax, owner, profit, and operating expense percentages and demonstrates how to parse the input string into a float64 for calculations. He shows how to set up the 'on divide income' callback to perform these calculations and update the UI with the results. The focus is on ensuring the app's functionality by correctly setting properties and handling data between the UI and Rust code.

20:08

🖥️ Compiling and Bundling the Rust Desktop Application

Travis guides viewers through the process of compiling the app with optimizations and creating a binary for easy access. He introduces 'cargo bundle', a tool for packaging the app into a format that can be added to the Mac Applications folder. The tutorial includes modifying the cargo.toml file to include package metadata and running the bundle command to create the final app package.

25:10

🎨 Adding Animation and Final Touches to the App

The final part of the script covers adding animations to the app for a better user experience. Travis demonstrates how to create a curtain-opening effect using rectangles and animation properties in Slint. He explains the process of toggling the visibility of results using a boolean property and animating the opening and closing of the result display. The video concludes with a call to action, encouraging viewers to subscribe and follow along for more tutorials.

Mindmap

Keywords

💡Rust

Rust is a systems programming language that focuses on safety, speed, and concurrency. It is known for its ability to prevent certain classes of bugs even during compile-time. In the video, Rust is used to build a desktop application, demonstrating its practical use for developing software with performance and reliability in mind. The script mentions learning Rust better and using it for business logic in the app.

💡Slint UI framework

Slint is a UI framework that allows developers to create user interfaces for applications. It is versatile and can be used with different programming languages, including Rust, which is the choice in the video. The framework is highlighted for its use in various industries, such as automotive and medical, and for its adoption by companies like SK Signate for electric vehicle charging solutions.

💡Income Divider

Income Divider is the name of the desktop application being developed in the video. It is designed to follow the 'profit first' methodology by dividing income into four parts: taxes, owner's pay, profit, and operating expenses. The app is a practical tool for the creator's business needs and serves as a project to improve Rust programming skills.

💡Profit First methodology

The Profit First methodology is a financial management approach that prioritizes profit by allocating a portion of incoming revenue to profit before covering other expenses. In the video, the creator uses this method to manage business finances and builds the Income Divider app to automate the division of income according to this approach.

💡Desktop App

A desktop app, as discussed in the video, is a software program designed to run on a personal computer rather than on a mobile device or web browser. The Income Divider is an example of a desktop app created to streamline the process of income division and financial management.

💡Business Logic

Business logic refers to the underlying processes and rules that drive the operations of an application or system. In the context of the video, the business logic is implemented in Rust and handles the calculations for dividing income into its respective categories within the Income Divider app.

💡Callbacks

In programming, a callback is a piece of executable code that is passed as an argument to other code. The video script mentions callbacks in the context of the Slint UI framework, where they are used to communicate changes in the state of the application, such as invoking the 'divide income' functionality when a button is clicked.

💡Cargo

Cargo is Rust's package manager and build system. It is used to manage Rust projects, including building the code, downloading libraries, and managing dependencies. In the video, Cargo is used to build and run the Income Divider app, showcasing its role in the development process.

💡UI Components

UI components are the individual elements that make up a graphical user interface, such as buttons, text fields, and sliders. The video script describes creating various UI components in the Slint framework, including a grid layout, text elements, line edits, and buttons, to construct the interface of the Income Divider app.

💡Animation

Animation in the context of UI development refers to the movement or change in properties of UI elements over time. The video describes using animation to create a 'curtain opening' effect when the results of the income division are displayed in the Income Divider app, enhancing the user experience.

Highlights

Travis from Travis.media is building a simple desktop app called 'Income Divider' using Rust and the Slint UI framework.

The 'Income Divider' app follows the profit-first methodology, dividing income into four parts for taxes, owner pay, profit, and operating expenses.

Travis aims to make the income division process less tedious by automating calculations through the app.

Slint is a UI framework that allows for the use of Rust, C++, or Node.js for business logic, chosen by Travis for its flexibility.

Slint supports various platforms including embedded apps, desktop apps, and industrial applications.

The app development process includes setting up a project with cargo generate and using the Slint project template.

The tutorial provides a step-by-step guide for beginners to learn how to build desktop apps with Rust and Slint.

Travis demonstrates the process of creating a UI layout using Slint's grid layout and built-in elements.

The importance of learning concepts over languages is emphasized for flexibility and adaptability in development.

Slint's documentation and VS Code extension facilitate the learning process for the UI framework.

Travis explains how to implement business logic in Rust, connecting it with the Slint UI components.

The app includes a feature to animate the display of results, adding an interactive element to the user experience.

The tutorial covers how to handle callbacks and state changes between the UI and Rust business logic.

Travis shows how to compile and run the app using Cargo, and how to create a release build.

The final app can be bundled into a macOS application for easy access and use.

The tutorial concludes with a demonstration of the complete app in action, showcasing its functionality.

Travis encourages viewers to subscribe for more content and provides a link to the GitHub repository for the app.

Transcripts

play00:00

hey everyone Travis here from travis.

play00:01

media today we're going to build a

play00:03

simple desktop app using rust in the

play00:05

slint UI framework now I don't really

play00:07

care these days to build meaningless

play00:09

things that I can't use or that I can't

play00:11

offer to the public to use I'm aiming

play00:13

for practical and I want to learn rust

play00:15

better this year so I built this little

play00:17

simple desktop app called income divider

play00:20

and all it does when I get paid or I

play00:22

receive some money in my business I like

play00:24

to follow the profit first methodology

play00:26

or a modified version of it and what I

play00:28

do is I take the money and I divide it

play00:30

up into four parts so let's say someone

play00:32

sent me

play00:34

3,722 bucks what I would do in the past

play00:36

is I would open up my calculator on my

play00:38

MacBook and I would type it in 3722 and

play00:41

then I'd multiply it by 3 for taxes and

play00:45

I'd find some piece of paper around my

play00:46

desk and I'd write it down i' do the

play00:48

calculations this way but I found that

play00:49

to be tedious and I found that it makes

play00:51

a big mess on my desk so I just created

play00:53

this desktop app and all I have to do

play00:54

like I said is put in the number click

play00:56

calculate and I get the amount I owe in

play00:59

taxes the the amount that I'm eventually

play01:01

going to pay myself out of it the amount

play01:02

of profit I want to keep in my business

play01:04

and the amount of operating expenses I

play01:06

also want to allocate so it gave me a

play01:08

reason to practice my rust and to learn

play01:10

to build desktop apps with it and I

play01:12

looked around a little bit and I chose

play01:14

slint to build it with now slint is a UI

play01:16

framework that supplies the user

play01:18

interface on your app and then you have

play01:20

the freedom to perform the business

play01:21

logic with either rust C++ or node.js

play01:25

I'm going to be using rust so as far as

play01:27

slint there are a lot of use cases

play01:29

there's embedded apps desktop apps

play01:31

industrial consumer Automotive so slint

play01:34

built with Russ now supports q andx and

play01:36

is being used for building instrument

play01:38

clusters for motorcycles and two-

play01:40

wheelers and then medical they have some

play01:41

videos on it being used with a Raspberry

play01:43

Pi supported boards and then there's one

play01:45

case story I found interesting in that

play01:47

SK signate the number one electric

play01:50

vehicle charging solution in the US has

play01:52

transitioned their human machine

play01:53

interface for their EV Chargers from QT

play01:57

to slint if you look here looks really

play01:58

nice really modern I don't have an

play02:00

electric vehicle I don't know much about

play02:02

it but it looks good but what I'm

play02:03

getting at is it isn't just some side

play02:05

hobby app that nobody uses it's pretty

play02:07

well known so I chose to build the app

play02:08

with that and just to note slint didn't

play02:10

sponsor this video I have no affiliation

play02:12

with them I just picked them based on

play02:13

what I read now if you scroll up and go

play02:15

to docs you can choose between rust C++

play02:19

and node.js so pick your tool and let's

play02:21

get started I'm going to be using rust

play02:23

so I'm going to click on the project

play02:25

template I'm going to open it in a new

play02:26

tab and by the way this tutorial is

play02:29

really good for learning how all this

play02:30

works so if you go to your project

play02:32

template which is a GitHub page and just

play02:34

follow the instructions so first step is

play02:36

install cargo generate I've already done

play02:38

that the next step is to create a new

play02:40

project with the template so I'm going

play02:42

to copy this open my terminal go to my

play02:44

repos folder and copy that command and

play02:46

I'll change my project to something like

play02:49

income app CD into that cargo

play02:52

build and then cargo

play02:55

run and there's your app the default app

play02:57

is just a counter so counter equals 4 42

play03:00

I can increase it by clicking here

play03:02

that's all it is but let's open it up in

play03:04

vs code and you have your normal files

play03:06

like your cargo Tomo file your cargo

play03:09

lock but what we're mainly interested in

play03:11

is this Source folder and the UI folder

play03:14

in your Source folder you have the main.

play03:16

RS this is where your business logic is

play03:18

going to live and we'll come to that in

play03:20

a minute we're going to start off with

play03:22

the slint part of it so if you go to the

play03:23

UI folder you have a file called app

play03:25

window. slint here the counter equals 42

play03:29

when the button is clicked it calls this

play03:31

request increase value which is this

play03:33

call back here then if you go to the

play03:35

Rust part of it you'll see that it gets

play03:37

the counter and it sets the counter to

play03:39

whatever it is plus one that's all so

play03:42

I'm actually going to comment this part

play03:44

out and let's go to app window. slint

play03:47

and just get rid of all this so you

play03:49

might be wondering like now I got to

play03:51

learn slint right it's great that you

play03:53

can use rust here but why I got to learn

play03:54

slant well here's the thing and this is

play03:56

some good advice for 2024 when it comes

play03:58

to tools like this you should be able to

play04:00

go to the documentation and get up to

play04:03

speed with slint pretty quickly if you

play04:05

haven't before you want to focus more

play04:06

this year on Concepts more than learning

play04:09

lots of languages and here's what I mean

play04:11

if we go back to the documentation and

play04:13

click on the slint

play04:15

language here's how you would approach

play04:17

this so here's a getting started page

play04:19

tells you hey use VSS code and then

play04:21

there's an extension go ahead and

play04:23

download that extension then you

play04:24

probably want to read the introduction

play04:25

under language reference and then look

play04:28

under Concepts you probably want to read

play04:30

these especially like positioning and

play04:32

layout of elements but the next section

play04:34

called syntax so you know what

play04:36

properties are in functions and

play04:38

callbacks and statements and conditional

play04:40

elements and repetitions or Loops are

play04:43

you know what these things are so when

play04:45

you're writing this app you should be

play04:46

able to in your head pseudo code

play04:49

everything and then come here and figure

play04:51

out how to do it in slint it's a good

play04:52

skill to have be conceptual so that you

play04:54

have flexibility between languages more

play04:57

than just learning a bunch of languages

play04:59

so I'm going to be be building this but

play05:00

we're going to be coming back here and

play05:01

referencing this syntax next you have

play05:04

some built-in stuff and some widgets so

play05:06

right here this is how you get buttons

play05:08

and checkboxes Sliders text edits

play05:10

vertical boxes all of that those are

play05:12

widgets and then if you want to know how

play05:14

these work there's a section called

play05:15

recipes and examples so animation

play05:18

examples layout examples Global

play05:19

callbacks custom widgets things you want

play05:21

to reference as you build so to start we

play05:23

may just want to go to layout examples

play05:26

and get an idea of like what this looks

play05:27

like so let's scroll up to the top

play05:30

and here here's a clickable button so we

play05:32

have a vertical box and inside of it we

play05:34

have a button it's kind of like flutter

play05:36

so I can click the button button press

play05:37

one time two time I can scroll down um

play05:41

and see animation

play05:43

examples States examples layout examples

play05:46

so here's three buttons in a vertical

play05:48

layout here's three buttons in a

play05:49

horizontal layout here's using grid box

play05:51

so we got two rows actually three rows

play05:54

the third row being a slider and you get

play05:55

an idea of how to do this so let's go

play05:58

back to our app let's think about this

play06:00

and you can open up any kind of

play06:02

diagramming tool like draw.io or

play06:04

something but basically just come in

play06:06

here what do you want the app to look

play06:07

like you can do this with a notepad as

play06:09

well but let's say I just want it to

play06:12

look like this I need some text here

play06:14

saying income divider make that bigger

play06:17

then I want an

play06:18

input then I want a button below it that

play06:21

just says calculate now if I go back and

play06:23

spend a few minutes in the documentation

play06:25

and go to something like uh under

play06:27

built-ins if you go to built-in elements

play06:29

you can find an item called grid layout

play06:31

you have access to columns rows and

play06:33

things like that so I could do a grid

play06:35

layout and have three rows in it so if

play06:37

you go back to our diagram here this

play06:39

would be a grid layout this is Row one

play06:41

row two and Row three so that they're

play06:43

stacked on top of each other let's just

play06:44

do that so let's go to our code and just

play06:47

type in Grid

play06:49

layout and within it let's create a

play06:51

couple

play06:52

rows Row one row

play06:55

two and Row

play06:58

three and in row one let's do some text

play07:02

here's some text and if you need to know

play07:04

what properties you can use with text

play07:06

it's all listed right

play07:07

here so I'm actually going to just copy

play07:10

this go to my code and paste that in

play07:12

inside of row and for text I'm going to

play07:15

do enter total

play07:17

income and remember I mentioned

play07:19

installing that vs code extension so I

play07:21

can say horizontal alignment and it

play07:23

shows me that that's available in a text

play07:25

element horizontal alignment

play07:28

center font size I'm going to make that

play07:31

big at like 24 pixels and font weight

play07:35

I'm going to make 900 I don't care

play07:36

anything about the color right now

play07:38

that's going to be our starting text now

play07:39

what about our input let's go back and

play07:42

take a look in addition to these

play07:43

built-in elements you have widgets so if

play07:46

you go to widgets and line edit you'll

play07:48

see that it's a widget used to enter a

play07:50

single line of text and here's an

play07:52

example here I can click on it I can

play07:54

type text in it so I'm going to use that

play07:55

so I'm going to copy

play07:57

this and paste that in my second row and

play08:00

I get this red squiggly because I need

play08:01

to import it and the easiest way to do

play08:04

that is just to highlight it start

play08:05

typing and accept the auto suggestion

play08:08

and you'll see it's added here at the

play08:09

top and here I'm going to have the

play08:11

placeholder text of ENT your

play08:14

number font size I'm going to make 16

play08:17

and I'll leave it at that then finally

play08:19

we need a button I think that's also

play08:21

under widgets yeah here's a

play08:24

button so this one's simple let just go

play08:28

back typ Tye in

play08:32

button and text is going to

play08:35

be

play08:38

calculate and that looks good for now

play08:40

and this vs code extension you can

play08:42

actually open your command pallet and

play08:44

there's an option that says slint Show

play08:46

preview if you click that it'll show you

play08:48

a preview of your app and there it is

play08:51

looks all jammed together so let's add

play08:53

some padding in this grid layout we can

play08:55

do padding 50 pixels and then also if we

play09:00

go back to builtins and built-in

play09:03

elements and grid layout I think they

play09:06

have a spacing element yeah spacing the

play09:08

distance between elements in the layout

play09:10

so let's also do spacing 25

play09:15

pixels and save that see what it looks

play09:17

like looks way better this enter your

play09:20

number let's Center that by doing uh

play09:23

horizontal alignment

play09:25

center see if that fixes it yep and then

play09:28

calculate this button is gray so let's

play09:30

go back to the documentation on that and

play09:33

there's a property called primary if set

play09:35

to true the button is displayed with the

play09:37

primary accent color so let's change it

play09:39

to primary

play09:41

true and that looks better as for the

play09:43

background color let's go to the main

play09:46

window and do background and I have a

play09:49

color 313 e50 which is like a dark blue

play09:54

change it to that and it looks better

play09:56

and then these look really narrow so

play09:58

let's go to the line edit and do height

play10:01

uh 35

play10:03

pixels and then the button Let's Do

play10:05

height of the same thing 35

play10:11

pixels and that looks great so like I

play10:14

said you just have to go and look at

play10:15

what properties each widget or built-in

play10:17

element has with it now that's the slint

play10:20

live preview let's actually compile it

play10:22

and see what it looks like so cargo

play10:25

run yeah and it looks about the same so

play10:28

that's good now in case you're wondering

play10:29

I didn't even talk about this but this

play10:31

is a slint file this is going to be your

play10:33

UI and you see here it exports component

play10:36

app window this can be named anything

play10:38

you want and it inherits the window

play10:41

component So within this you can do all

play10:43

your UI editing you can also create

play10:45

another uh component like I can do um

play10:48

component let's just do it uh component

play10:51

uh calculate calculate

play10:53

button and then here I can actually just

play10:56

put this button that I have here

play11:00

uh place it there and then down here I

play11:02

can just call calculate button and it's

play11:04

the same thing so you can make your app

play11:06

a lot cleaner by breaking things up a

play11:08

bit but we don't need to do that this is

play11:09

a small app so I'm going to undo that

play11:12

now we have the front end working but it

play11:14

does nothing so we need to jump over to

play11:16

the business logic which in my case is

play11:18

rust it might be C++ or no. JS for you

play11:21

but we need to jump into the back end

play11:22

and figure out how this all works so if

play11:25

we go to main. RS here's what's going on

play11:28

we have a main function it returns a

play11:29

result within that we're declaring a new

play11:32

app window so app window of course

play11:34

corresponds to our component it's called

play11:36

app window and then we create a weak

play11:39

pointer to it which is explained in the

play11:40

documentation if you don't know what

play11:42

that means and then you unwrap that weak

play11:44

pointer here in this closure but what is

play11:46

this ui. on request increase value well

play11:49

if you remember when we created this

play11:51

template if I go back to the GitHub repo

play11:53

there was a call back called request

play11:55

increase value and then when you click

play11:57

the button it called that call back now

play11:59

if we go to the documentation the slint

play12:01

documentation and we go to Concepts and

play12:04

or actually syntax and

play12:06

callbacks you'll see here that

play12:08

components May declare callbacks that

play12:10

communicate changes of the state to the

play12:13

outside callbacks are invoked by calling

play12:15

them like you would call a function so

play12:17

when this request increased value is

play12:19

called it's going to use this getter and

play12:21

Setter so get counter set counter to do

play12:24

its thing so if I go back to what was

play12:25

originally there you'll see that there's

play12:27

a property called counter so all you do

play12:30

in the business logic side is you add

play12:33

set to whatever that property was so set

play12:36

counter get counter or on to the call

play12:40

back so if you look at the documentation

play12:42

for rust the rust API and you scroll

play12:45

down you see here for each top level

play12:48

property there's a Setter and a getter

play12:50

so you can call set underscore whatever

play12:52

the property name is or getor property

play12:55

name same thing for each top level

play12:56

callback there's an invoke underscore

play12:59

callback name or an onore callback name

play13:02

and this documentation is really good

play13:04

it's done really well so what we want to

play13:06

do is when we click this button let's go

play13:08

back to the UI when we click this button

play13:11

which on button you can just say clicked

play13:13

there's a property called clicked you

play13:15

can call something so let's go here and

play13:17

go to properties and it's very easy it's

play13:19

just property the type and then the name

play13:21

of the property so I'm going to grab

play13:23

this and there's different qualifiers

play13:25

that specify how the property can be

play13:26

read and written there's a private which

play13:28

is theault default it can be an input an

play13:30

output or an inout so I'm just going to

play13:32

start with private so up here at the

play13:34

very top I'll create a new

play13:37

property the type is going to be a

play13:39

string and we'll call it results and

play13:42

I'll set it to an empty string this is

play13:45

going to be our results when we click

play13:46

the button we want to display that

play13:48

calculated income in a window at the

play13:50

bottom next when we click this button we

play13:53

want to invoke this call back which we

play13:54

hadn't created yet called divide

play13:57

income inside of it we want to put

play14:00

whatever input that we put in this line

play14:02

edit so up here under line edit let's

play14:04

just call you can actually assign

play14:06

variables to this so I can say income

play14:09

and then it's kind of like a go syntax

play14:11

here but income equals this line edit

play14:15

component so down here I can just do

play14:17

income. text I'm going to pass in the

play14:21

number that we input basically and then

play14:23

as far as callbacks if we go to the

play14:25

documentation and callbacks you'll see

play14:28

an example

play14:29

here of call back of hello and then

play14:31

you're passing in some kind of arguments

play14:33

so up here under this property we'll

play14:34

just put our call back and we'll call it

play14:37

divide income and we just need to pass

play14:40

in a string value great so when we click

play14:43

this button we invoke this divide income

play14:45

call back which is up here and we're

play14:47

going to do some calculations and

play14:49

display them in results so let's create

play14:51

a little place in the bottom to display

play14:52

these results so let's create a new row

play14:55

and inside of it create a vertical box

play14:59

and in that a

play15:01

rectangle let's do a height of 100

play15:06

pixels a background of uh let's do FS2

play15:10

F2

play15:11

F2 and that looks pretty good and then

play15:14

inside of that rectangle let's do a text

play15:17

component or widget I guess is the

play15:19

proper term with a color of black the

play15:22

text should be black let's make the font

play15:25

size uh 16 pixels and font weight a

play15:29

little bigger at

play15:31

500 and then the text itself should be

play15:34

the results right so to access this

play15:37

results property since it's outside of

play15:39

this grid layout in the rows and all of

play15:41

that we can do root dot

play15:45

results this is all in the documentation

play15:48

root gives you access to that root

play15:49

component the root part of the component

play15:51

then you can also use self for the

play15:53

component itself all right so I think

play15:55

we're ready to go over to rust and make

play15:56

this magic happen so here at the top I'm

play15:58

going to give my calculations const uh

play16:01

the tax

play16:02

percentage is going to be a float

play16:06

64 and is going to be 0.30 so 30% copy

play16:11

this the owner percentage will be

play16:16

55 the profit percentage will be

play16:22

5% and the operating expenses percentage

play16:27

will be 10% and that should equal 100 60

play16:31

7 90 100 yeah 100% And so here instead

play16:34

of on request increased value we'll do

play16:37

on on on divide income so it already

play16:41

recognizes that call back that's

play16:42

available to me so what do we need to do

play16:44

now well if you go back you see that we

play16:46

actually pass in a string to that divide

play16:48

income and just a note in slint it's

play16:50

going to be divide Dash income but when

play16:52

you get over to rust it's going to use

play16:54

underscores and it knows the difference

play16:56

there so save this that's that's fine on

play16:58

divide income so what I need to do is I

play17:00

need to pass in the string and that

play17:02

string is actually going to be a number

play17:04

so let's parse it let

play17:07

num of float 64 equals string. trim

play17:12

always got to trim first dop parse do

play17:17

unwrap so we're going to turn that

play17:18

string into a float 64 then we need to

play17:20

get the percentages of this number so

play17:23

let tax of

play17:26

f64 equal num

play17:28

times uh tax

play17:31

percentage copy that four

play17:34

times let's do owner num times owner

play17:41

percentage profit num times profit

play17:46

percentage then operating expenses Opex

play17:50

num

play17:51

times operating expenses

play17:55

percentage save that and actually we

play17:58

didn't put air parenthesis here so

play18:00

that's a

play18:01

problem and the same thing with unwrap

play18:03

what are we doing and then let's let

play18:07

result equals we'll do a

play18:12

format here we'll do

play18:16

taxes new line

play18:21

owner new

play18:24

line

play18:27

profit

play18:28

new line and operating

play18:34

expenses and then our arguments so

play18:38

one 2 3

play18:42

4 TX

play18:45

owner

play18:47

profit and operating

play18:50

expenses save it and this needs to be a

play18:53

decimal place of two so inside of these

play18:57

we'll put

play19:00

colon 2 I think that does it let me word

play19:02

wrap here we go and now I should be able

play19:05

to use that Setter we talked about

play19:07

earlier ui. set what are we setting the

play19:10

results property to

play19:14

result and we seem to have an error here

play19:17

if I hover over it it says method set

play19:19

results is private so we need to go back

play19:20

to our slint file and I need to set this

play19:24

property which is private by default to

play19:26

be an input by putting in in front of it

play19:28

remember there's an input there's an

play19:29

output and then there's an input output

play19:32

so I think doing an input should be

play19:33

enough if I go back there's still an

play19:36

error and if this happens just resave

play19:39

it and that fixed that now we have an

play19:42

issue with result and it tells us here

play19:44

that we need to add into to this result

play19:46

so result. into that's in the

play19:49

documentation if you're wondering what

play19:50

that does but now when I save it we

play19:52

should be good I can now set the result

play19:54

to whatever we calculate here then in

play19:57

air slint file that should show up down

play20:00

here in the text of root results so

play20:02

let's try it out and let's actually run

play20:04

the app cargo

play20:08

run and let's do like 4,000 actually

play20:11

let's do 100 because it's easy to

play20:12

calculate click calculate and we get the

play20:15

right numbers but we have this weird

play20:17

decimal thing going on I thought we

play20:18

fixed that so if we go back to main. RS

play20:21

it's actually colon.

play20:26

two that should do it

play20:28

let's rebuild

play20:30

this and try it out again so let's do

play20:33

1,00 calculate there we have it 300 550

play20:37

50 100 great so at this point you have a

play20:40

fully working app you can do uh cargo

play20:43

build

play20:46

release to get you an optimized binary

play20:48

and then of course we don't want to have

play20:50

to find this binary every time we want

play20:51

to run it I would rather make it an app

play20:53

on my Mac so that I can find it in my

play20:56

Spotlight income divider there it is and

play20:59

to do that we use cargo bundle so to get

play21:01

that working we need to run cargo

play21:03

install cargo bundle so here in my app

play21:05

I'll do that cargo install cargo bundle

play21:09

and then according to the documentation

play21:10

we need to add a package metadata bundle

play21:13

to our cargo. Tomo file so here at the

play21:15

bottom let's add that and then we'll

play21:18

need a name an identifier and a

play21:21

description and let copy this identifier

play21:23

this GitHub identifier so name equals

play21:27

income device

play21:32

identifier

play21:34

equals I'm going to change my username

play21:36

to my username and then my project on

play21:40

GitHub is this so I'm going to copy this

play21:42

project name paste that in here and then

play21:44

finally we need to add a description to

play21:46

the package so description equals a

play21:49

desktop

play21:51

app that divides up

play21:57

income

play21:58

and then the final step is to run cargo

play22:00

bundle with the release flag hit

play22:03

return now if I look in my target folder

play22:06

in in release I have a bundle folder

play22:08

open that actually let's open it up in

play22:11

finder open up bundle OSX and there's my

play22:15

app I simply just drag that to

play22:18

Applications but let's take this a step

play22:20

further and add this animation so if I

play22:22

put 1,000 when I click calculate we can

play22:24

set it so a window opens so click

play22:27

calculate and this window opens to our

play22:29

numbers this will teach you how to use

play22:31

the animation so let's go back to app

play22:33

window and under the rectangle that

play22:36

shows a results let's create two more

play22:37

rectangles let's call this left

play22:40

rectangle and then right

play22:43

rectangle you'll see why in a

play22:46

minute and

play22:49

rectangle for that

play22:51

one and the rectangle for our left one

play22:55

now the background for these let's make

play22:57

as almost black so 3 3 3 3 3

play23:02

3 paste that also on the left the right

play23:05

rectangle so what's going to happen here

play23:07

is you have two rectangles that are

play23:08

opening like a curtain so this first

play23:10

rectangle starts out as

play23:12

x0 it starts out right at the zero

play23:15

position think of X like 0 to 100 or

play23:17

whatever x0 is going to be all the way

play23:20

out the picture and then when we click

play23:21

the button we want it to come in halfway

play23:23

so we can do like parent width divided

play23:25

two and then when we click it again it

play23:27

goes back to zero so it's just this left

play23:29

panel coming in and coming out and when

play23:31

we do the right panel we needed to start

play23:33

at the end of it and come in halfway and

play23:36

back out halfway so let's try this let's

play23:39

go up here and let's create another

play23:41

property let's do in

play23:44

out and it's going to be a type of

play23:47

bull and let's call it open

play23:51

curtain cuz the curtain is being open

play23:53

what we do is when we click on it we're

play23:55

going to set that to True when we click

play23:56

on it again it's going to set to false

play23:58

and it's just going to be a toggle so in

play23:59

our first rectangle when it comes to

play24:01

width we're going to put open curtain if

play24:03

that's false the width is going to be

play24:05

zero but if it's true the width is going

play24:07

to be parent. width which is going to be

play24:10

the full width of the parent divided

play24:13

two that's going to take it to the

play24:15

halfway position and then for height we

play24:17

want it the full height of the parent so

play24:19

parent.

play24:21

height and then for animate let's go to

play24:26

animations and there's a whole bunch of

play24:28

animations here these values here and

play24:30

work with that so we're want to animate

play24:33

the

play24:33

width and let's put these values in here

play24:37

for duration let's do 250 milliseconds

play24:40

and for

play24:41

easing let's do ease in and that should

play24:45

do it now the right rectangle is a

play24:47

little bit trickier so the X if open

play24:50

curtain is

play24:52

false then the EXP position should be

play24:54

outside of the right side which is going

play24:56

to be the full width of the parent so

play24:59

parent. width else it needs to be

play25:02

parent. width /

play25:05

two which is going to bring it in

play25:07

halfway that's the EXP position now the

play25:10

width needs to be if open

play25:14

curtain then the width needs to be zero

play25:16

pixels meaning the x is going to be all

play25:18

the way to the right and it's not going

play25:19

to have any width at all but if that's

play25:21

true it also needs to be parent

play25:24

width / two height is going to be

play25:28

parent. height and then we need to

play25:31

animate two things this

play25:33

time so animate width it's going to be

play25:37

like the last one duration 250 EAS in

play25:40

then we also need to animate the

play25:43

X to the same thing let's run that and

play25:46

try it out cargo

play25:48

run and so we have our window here

play25:50

remember our Windows almost black let's

play25:53

put in a number like 5,000 when we click

play25:56

calculate they should open to our

play25:58

numbers and nothing happens because we

play26:01

didn't set the click so let's go find

play26:03

our

play26:04

button clicked we want two things to

play26:07

happen before we actually divide the

play26:10

income and here we can do root dot open

play26:15

curtain is equal to not root. openen

play26:21

curtain so again this is just a toggle

play26:23

every time we click this it's going to

play26:24

flip to the other thing false true false

play26:26

true so let's save it try it again type

play26:29

in a number like 5,000 click calculate

play26:33

and it opens click calculate again it

play26:35

closes so that's pretty neat and guys

play26:38

this is basically the tip of the iceberg

play26:40

there's so much you can do here you can

play26:42

actually access elements on your

play26:44

computer and you can get way more

play26:46

advanced and intricate than this but

play26:47

this is just a simple app a way for me

play26:49

to speed up things in my business and

play26:52

also a way for me to practice using rust

play26:54

which is a goal I have this year I'll

play26:56

link to the GitHub repo below if got

play26:57

stuck if you found this video helpful

play26:59

you like this kind of thing give it a

play27:01

thumbs up if you haven't subscribed to

play27:02

the channel consider doing so and I'll

play27:04

see you in the next

play27:19

video

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Rust ProgrammingDesktop AppSlint UIIncome DividerProfit FirstBusiness ToolUI DevelopmentCode TutorialApp BundleRust PracticeAnimation Effects
¿Necesitas un resumen en inglés?