Skip to content

Commit

Permalink
fix for web
Browse files Browse the repository at this point in the history
  • Loading branch information
waozixyz committed Jan 22, 2025
1 parent 0b5e9d6 commit bf4318e
Show file tree
Hide file tree
Showing 25 changed files with 251 additions and 87 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Build and Deploy Web Version

on:
push:
branches: [ axis ] # Trigger on pushes to the `axis` branch
pull_request:
branches: [ axis ] # Trigger on PRs to the `axis` branch

jobs:
build-and-deploy:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the repository
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: 'recursive' # Ensure submodules are checked out

# Step 2: Set up Emscripten (for WASM compilation)
- name: Set up Emscripten
uses: mymindstorm/setup-emsdk@v12
with:
version: '3.1.45' # Latest stable version of Emscripten

# Step 3: Install xmake
- name: Install xmake
run: |
wget https://xmake.io/shget.text -O - | bash
source ~/.xmake/profile
# Step 4: Build the project for WASM
- name: Build for Web (WASM)
run: |
xmake f -p wasm -y
xmake -y
# Step 5: Prepare build artifacts for Netlify
- name: Prepare artifacts
run: |
mkdir -p netlify-build
cp -r build/clay/* netlify-build/
cp index.html netlify-build/
cp manifest.json netlify-build/
cp -r fonts netlify-build/
cp -r images netlify-build/
# Step 6: Deploy to Netlify
- name: Deploy to Netlify
uses: nwtgck/actions-netlify@v2
with:
publish-dir: './netlify-build' # Directory containing the built files
production-deploy: true # Deploy to production
github-token: ${{ secrets.GITHUB_TOKEN }}
netlify-auth-token: ${{ secrets.NETLIFY_AUTH_TOKEN }} # Netlify authentication token
netlify-site-id: ${{ secrets.NETLIFY_SITE_ID }} # Netlify site ID
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}

3 changes: 3 additions & 0 deletions include/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

#ifdef __EMSCRIPTEN__
void InitializePages(void);

extern void measureTextFunction(Clay_String *text, Clay_TextElementConfig *config);
extern void queryScrollOffsetFunction(Clay_ElementId elementId);
#else
void InitializePages(SDL_Renderer* renderer);
void HandleSDLEvents(bool* running);
Expand Down
2 changes: 1 addition & 1 deletion include/components/calendar_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdbool.h>
#include "../clay_extensions.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"
#include "../config.h"
#include <stdio.h>
#include <math.h>
Expand Down
2 changes: 1 addition & 1 deletion include/components/color_picker.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef COLOR_PICKER_H
#define COLOR_PICKER_H
#include "../clay_extensions.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"
#include <stdio.h>
#include "../config.h"
#include "modal.h"
Expand Down
2 changes: 1 addition & 1 deletion include/components/date_picker.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <time.h>
#include "../clay_extensions.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"
#include "modal.h"
#include <string.h>

Expand Down
2 changes: 1 addition & 1 deletion include/components/home_card.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdint.h>
#include "../clay_extensions.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"
#include "../config.h"


Expand Down
2 changes: 1 addition & 1 deletion include/components/modal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define MODAL_H

#include "../clay_extensions.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"
#include "../config.h"

typedef struct {
Expand Down
2 changes: 1 addition & 1 deletion include/components/nav.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "../config.h"
#include "../clay_extensions.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"
#include <string.h>

#ifndef __EMSCRIPTEN__
Expand Down
2 changes: 1 addition & 1 deletion include/components/text_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define TEXT_INPUT_H

#include "../clay_extensions.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"
#include "../config.h"
#include <string.h>
#include <stdlib.h>
Expand Down
4 changes: 2 additions & 2 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#define CONFIG_H

#include "clay_extensions.h"
#include "../vendor/clay/clay.h"

#include "clay.h"
#include <stdio.h>
// Window & UI configuration
extern double windowWidth, windowHeight;
extern float globalScalingFactor;
Expand Down
2 changes: 1 addition & 1 deletion include/pages/habits.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define HABITS_H

#include "../clay_extensions.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"
#include "../state/habits_state.h"
#include "../config.h"
#include "../components/modal.h"
Expand Down
2 changes: 1 addition & 1 deletion include/pages/todos.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define TODOS_H

#include "../clay_extensions.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"
#include "../components/text_input.h"
#include "../config.h"
#include "../state/todos_state.h"
Expand Down
2 changes: 1 addition & 1 deletion include/state/habits_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <stdio.h>
#include <string.h>
#include "../config.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"
#include "../components/text_input.h"

#define MAX_CALENDAR_DAYS 1000
Expand Down
2 changes: 1 addition & 1 deletion include/state/todos_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdint.h>
#include <time.h>
#include <stdlib.h>
#include "../../vendor/clay/clay.h"
#include "clay.h"
#include <string.h>
#include <stdio.h>
#include "../components/text_input.h"
Expand Down
1 change: 1 addition & 0 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>


size_t strlcpy(char *dst, const char *src, size_t dstsize);
Expand Down
76 changes: 72 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,74 @@

const importObject = {
env: {
// Add this function to convert a tm struct to a Unix timestamp
_mktime_js: (tmPtr) => {
// Read the tm struct from memory
const tm_sec = memoryDataView.getInt32(tmPtr, true); // Seconds (0-59)
const tm_min = memoryDataView.getInt32(tmPtr + 4, true); // Minutes (0-59)
const tm_hour = memoryDataView.getInt32(tmPtr + 8, true); // Hours (0-23)
const tm_mday = memoryDataView.getInt32(tmPtr + 12, true); // Day of the month (1-31)
const tm_mon = memoryDataView.getInt32(tmPtr + 16, true); // Month (0-11, 0 = January)
const tm_year = memoryDataView.getInt32(tmPtr + 20, true); // Year (years since 1900)
const tm_wday = memoryDataView.getInt32(tmPtr + 24, true); // Day of the week (0-6, 0 = Sunday)
const tm_yday = memoryDataView.getInt32(tmPtr + 28, true); // Day of the year (0-365)
const tm_isdst = memoryDataView.getInt32(tmPtr + 32, true); // Daylight saving time flag

// Convert the tm struct to a JavaScript Date object
const date = new Date(
tm_year + 1900, // Year (add 1900 to get the full year)
tm_mon, // Month (0-11)
tm_mday, // Day of the month (1-31)
tm_hour, // Hours (0-23)
tm_min, // Minutes (0-59)
tm_sec // Seconds (0-59)
);

// Convert the Date object to a Unix timestamp (seconds since the epoch)
const timestamp = Math.floor(date.getTime() / 1000);

return timestamp; // Return the Unix timestamp
},
// Add this function to handle heap resizing
emscripten_resize_heap: (requestedSize) => {
console.log(`Resizing heap to ${requestedSize} bytes`);
const oldMemory = importObject.env.memory;
const oldBuffer = oldMemory.buffer;

// Calculate the new size (round up to the nearest 64KB page)
const PAGE_SIZE = 65536; // 64KB
const newSize = Math.ceil(requestedSize / PAGE_SIZE) * PAGE_SIZE;

// Create a new memory instance with the new size
const newMemory = new WebAssembly.Memory({
initial: Math.ceil(newSize / PAGE_SIZE),
maximum: oldMemory.maximum,
});

// Copy the old memory contents to the new memory
const newBuffer = newMemory.buffer;
new Uint8Array(newBuffer).set(new Uint8Array(oldBuffer));

// Replace the old memory with the new memory
importObject.env.memory = newMemory;
memoryDataView = new DataView(newBuffer);

// Notify Emscripten that the memory has grown
if (importObject.env.emscripten_notify_memory_growth) {
importObject.env.emscripten_notify_memory_growth(0); // 0 is the memory index
}

return 1; // Return 1 to indicate success
},
_tzset_js: () => {
console.log("_tzset_js called (no-op)");
},
_emscripten_memcpy_js: (dest, src, num) => {
const destArray = new Uint8Array(importObject.env.memory.buffer, dest, num);
const srcArray = new Uint8Array(importObject.env.memory.buffer, src, num);
destArray.set(srcArray);
return dest;
},
emscripten_date_now: () => Date.now(),
memory: new WebAssembly.Memory({ initial: 256, maximum: 512 }),
table: new WebAssembly.Table({ initial: 0, element: 'anyfunc' }),
Expand Down Expand Up @@ -745,7 +813,7 @@
console.error(error.stack);
}
},
addTodoFunction: (collectionPtr, textPtr) => {
JS_AddTodo: (collectionPtr, textPtr) => {
try {
const stored = localStorage.getItem("todosData");
let todosData = stored ? JSON.parse(stored) : { todos: [], active_day: "Monday" };
Expand Down Expand Up @@ -775,7 +843,7 @@
}
},

deleteTodoFunction: (collectionPtr, todoId) => {
JS_DeleteTodo: (collectionPtr, todoId) => {
try {
const stored = localStorage.getItem("todosData");
if (!stored) return;
Expand All @@ -795,7 +863,7 @@
}
},

toggleTodoFunction: (collectionPtr, todoId) => {
JS_ToggleTodo: (collectionPtr, todoId) => {
try {
const stored = localStorage.getItem("todosData");
if (!stored) return;
Expand All @@ -811,7 +879,7 @@
}
},

setActiveDayFunction: (collectionPtr, dayPtr) => {
JS_SetActiveDay: (collectionPtr, dayPtr) => {
try {
const stored = localStorage.getItem("todosData");
let todosData = stored ? JSON.parse(stored) : { todos: [], active_day: "Monday" };
Expand Down
2 changes: 1 addition & 1 deletion run_web.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ fi

echo "Watching for changes in $ROOT_DIR/src and index.html"
while inotifywait -r -e modify "$ROOT_DIR/src" "$ROOT_DIR/index.html"; do
make clean && make all
xmake f -p wasm && xmake
done
11 changes: 10 additions & 1 deletion src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


#include "clay_extensions.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"
bool pages_initialized = false;

#ifdef __EMSCRIPTEN__
Expand All @@ -12,6 +12,15 @@ void InitializePages() {
InitializeTodosPage();
pages_initialized = true;
}

void measureTextFunction(Clay_String *text, Clay_TextElementConfig *config) {
// Empty implementation - actual measurement happens in JS
}

void queryScrollOffsetFunction(Clay_ElementId elementId) {
// Empty implementation - actual query happens in JS
}

#else
void InitializePages(SDL_Renderer* renderer) {
if (pages_initialized) return;
Expand Down
10 changes: 5 additions & 5 deletions src/components/nav.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ typedef struct {
} NavIcon;

static NavIcon NAV_ICONS[] = {
{.url = CLAY_STRING("icons/home.png"), .dimensions = {24, 24}},
{.url = CLAY_STRING("icons/habits.png"), .dimensions = {24, 24}},
{.url = CLAY_STRING("icons/todos.png"), .dimensions = {24, 24}},
{.url = CLAY_STRING("icons/timeline.png"), .dimensions = {24, 24}},
{.url = CLAY_STRING("icons/routine.png"), .dimensions = {24, 24}}
{.url = CLAY_STRING("images/icons/home.png"), .dimensions = {24, 24}},
{.url = CLAY_STRING("images/icons/habits.png"), .dimensions = {24, 24}},
{.url = CLAY_STRING("images/icons/todos.png"), .dimensions = {24, 24}},
{.url = CLAY_STRING("images/icons/timeline.png"), .dimensions = {24, 24}},
{.url = CLAY_STRING("images/icons/routine.png"), .dimensions = {24, 24}}
};

#ifndef __EMSCRIPTEN__
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define CLAY_IMPLEMENTATION

#include "clay_extensions.h"
#include "../../vendor/clay/clay.h"
#include "clay.h"

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
Expand Down
10 changes: 5 additions & 5 deletions src/pages/habits.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ static SDL_Texture* trash_texture = NULL;
void InitializeHabitTabBar(SDL_Renderer* renderer) {
SDL_Surface* surface;

surface = load_image("icons/check.png");
surface = load_image("images/icons/check.png");
check_texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);

surface = load_image("icons/edit.png");
surface = load_image("images/icons/edit.png");
edit_texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);

surface = load_image("icons/trash.png");
surface = load_image("images/icons/trash.png");
trash_texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
}
Expand Down Expand Up @@ -385,7 +385,7 @@ static void RenderHabitHeader() {
}),
CLAY_IMAGE({
.sourceDimensions = { 24, 24 },
.sourceURL = CLAY_STRING("icons/trash.png")
.sourceURL = CLAY_STRING("images/icons/trash.png")
})) {}
#else
CLAY(CLAY_LAYOUT({
Expand Down Expand Up @@ -416,7 +416,7 @@ static void RenderHabitHeader() {
}),
CLAY_IMAGE({
.sourceDimensions = { 24, 24 },
.sourceURL = CLAY_STRING("icons/check.png")
.sourceURL = CLAY_STRING("images/icons/check.png")
})) {}
#else
CLAY(CLAY_LAYOUT({
Expand Down
Loading

0 comments on commit bf4318e

Please sign in to comment.