Electron অ্যাপ্লিকেশনে Platform-Specific কোড এবং কনফিগারেশন ব্যবহৃত হয় যখন আপনার অ্যাপ্লিকেশনটি বিভিন্ন প্ল্যাটফর্মে (যেমন Windows, macOS, Linux) চলবে এবং প্রতিটি প্ল্যাটফর্মের জন্য কিছু নির্দিষ্ট আচরণ বা ফিচার প্রয়োজন হয়। উদাহরণস্বরূপ, উইন্ডোজের জন্য মেনু কনফিগারেশন আলাদা হতে পারে, অথবা macOS-এ ডিফল্ট অ্যাপ্লিকেশন স্টাইল বা ফাইল পাথ পার্থক্য থাকতে পারে।
১. Platform-Specific কোড ব্যবহারের জন্য process.platform
Electron এর process.platform ভেরিয়েবল ব্যবহার করে আপনি প্ল্যাটফর্মের উপর ভিত্তি করে কোড লিখতে পারেন। এটি বিভিন্ন অপারেটিং সিস্টেম (OS) যেমন Windows, macOS, এবং Linux এর মধ্যে পার্থক্য নির্ধারণ করতে সাহায্য করে।
উদাহরণ: process.platform ব্যবহার করা
const platform = process.platform;
if (platform === 'win32') {
console.log('This is Windows');
// উইন্ডোজ প্ল্যাটফর্মের জন্য কোড
} else if (platform === 'darwin') {
console.log('This is macOS');
// macOS প্ল্যাটফর্মের জন্য কোড
} else if (platform === 'linux') {
console.log('This is Linux');
// Linux প্ল্যাটফর্মের জন্য কোড
}
process.platform:win32: Windowsdarwin: macOSlinux: Linux
এটি আপনাকে প্ল্যাটফর্ম ভিত্তিক কাস্টম কোড যুক্ত করার সুযোগ দেয়।
২. Platform-Specific কনফিগারেশন
আপনি Electron অ্যাপ্লিকেশনের কনফিগারেশনে প্ল্যাটফর্ম নির্দিষ্ট কিছু সেটিংস যুক্ত করতে পারেন। যেমন, কিছু ফিচার বা ডিফল্ট আচরণ নির্দিষ্ট প্ল্যাটফর্মে কার্যকর হতে পারে। উদাহরণস্বরূপ, macOS এ অ্যাপ্লিকেশন মেনু সাধারণত app এ থাকে, কিন্তু Windows এবং Linux এ মেনু উইন্ডোর সাথে থাকে।
১. Platform-Specific মেনু কনফিগারেশন
const { app, Menu, BrowserWindow } = require('electron');
let mainWindow;
app.on('ready', () => {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
});
mainWindow.loadFile('index.html');
// প্ল্যাটফর্ম নির্দিষ্ট মেনু কনফিগারেশন
const menuTemplate = [
{
label: 'File',
submenu: [
{ role: 'quit' }
]
}
];
// macOS এর জন্য কাস্টম মেনু
if (process.platform === 'darwin') {
menuTemplate.unshift({
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'quit' }
]
});
}
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
});
- Windows/Linux: অ্যাপ্লিকেশনের মেনু সাধারণত উইন্ডোতে দেখানো হয়।
- macOS: অ্যাপের মেনু
app.nameএর নামের সাথে থাকে এবং আরও কিছু স্ট্যান্ডার্ড মেনু (যেমনaboutএবংquit) থাকে।
৩. Platform-Specific ফাইল পাথ
প্রতিটি প্ল্যাটফর্মে ফাইল সিস্টেমের পথ আলাদা হতে পারে, যেমন Windows-এ ফাইল পাথ C:\\Users\\Username\\Documents\\ হবে, তবে macOS এবং Linux-এ /Users/Username/Documents/ হবে। আপনি path মডিউল ব্যবহার করে এই পার্থক্য হ্যান্ডেল করতে পারেন।
উদাহরণ: Platform-Specific ফাইল পাথ
const path = require('path');
let filePath;
if (process.platform === 'win32') {
filePath = path.join('C:', 'Users', 'Username', 'Documents', 'myfile.txt');
} else {
filePath = path.join('/Users', 'Username', 'Documents', 'myfile.txt');
}
console.log(filePath); // প্ল্যাটফর্ম অনুযায়ী ফাইল পাথ প্রিন্ট করবে
৪. Platform-Specific নোটিফিকেশন
ইলেকট্রন অ্যাপ্লিকেশনে প্ল্যাটফর্ম অনুসারে নোটিফিকেশন কনফিগারেশন আলাদা হতে পারে। macOS এবং Windows এর জন্য আলাদা স্টাইলের নোটিফিকেশন হতে পারে। আপনি Notification API ব্যবহার করে প্ল্যাটফর্ম অনুসারে নোটিফিকেশন কাস্টমাইজ করতে পারেন।
উদাহরণ: Platform-Specific নোটিফিকেশন
const { Notification } = require('electron');
if (process.platform === 'darwin') {
new Notification({ title: 'macOS Notification', body: 'This is a macOS notification' }).show();
} else if (process.platform === 'win32') {
new Notification({ title: 'Windows Notification', body: 'This is a Windows notification' }).show();
} else {
new Notification({ title: 'Linux Notification', body: 'This is a Linux notification' }).show();
}
এভাবে আপনি প্ল্যাটফর্ম অনুসারে আলাদা আলাদা নোটিফিকেশন শো করতে পারেন।
৫. Platform-Specific অ্যাপ্লিকেশন আইকন
একই অ্যাপ্লিকেশনকে বিভিন্ন প্ল্যাটফর্মে আলাদা আইকন প্রদান করতে পারেন। macOS, Windows, এবং Linux এর জন্য আলাদা আইকন ব্যবহার করতে পারেন।
উদাহরণ: Platform-Specific আইকন
const { app, BrowserWindow } = require('electron');
const path = require('path');
let mainWindow;
app.on('ready', () => {
const iconPath = process.platform === 'win32' ? 'icon.ico' : path.join(__dirname, 'icon.png');
mainWindow = new BrowserWindow({
width: 800,
height: 600,
icon: iconPath // Platform অনুযায়ী আইকন নির্ধারণ
});
mainWindow.loadFile('index.html');
});
এখানে, Windows এর জন্য .ico ফাইল এবং macOS এবং Linux এর জন্য .png ফাইল ব্যবহার করা হয়েছে।
৬. Platform-Specific প্যাকেজিং এবং ডিস্ট্রিবিউশন
Electron অ্যাপ্লিকেশন তৈরি করার পর আপনি electron-packager বা electron-builder ব্যবহার করে প্যাকেজিং এবং ডিস্ট্রিবিউশন করতে পারেন। এগুলির মাধ্যমে প্ল্যাটফর্ম নির্দিষ্ট কনফিগারেশন এবং ফাইল তৈরি করা যায়।
উদাহরণ: electron-builder এর মাধ্যমে প্ল্যাটফর্ম অনুযায়ী প্যাকেজিং
{
"build": {
"appId": "com.example.app",
"productName": "MyApp",
"mac": {
"target": "dmg"
},
"win": {
"target": "nsis"
},
"linux": {
"target": "AppImage"
}
}
}
এখানে, macOS, Windows, এবং Linux এর জন্য আলাদা target ফরম্যাট নির্বাচন করা হয়েছে।
সারাংশ
- Platform-Specific কোড:
process.platformব্যবহার করে প্ল্যাটফর্ম নির্দিষ্ট কোড লিখে অ্যাপের আচরণ কাস্টমাইজ করা যায়। - Platform-Specific কনফিগারেশন: মেনু, ফাইল পাথ, নোটিফিকেশন এবং আইকন প্ল্যাটফর্ম অনুযায়ী কনফিগার করা হয়।
- Platform-Specific প্যাকেজিং: প্যাকেজিং এবং ডিস্ট্রিবিউশনের জন্য
electron-builderবাelectron-packagerব্যবহার করা হয়।
এই সব কনফিগারেশন দ্বারা আপনি বিভিন্ন প্ল্যাটফর্মে আপনার Electron অ্যাপ্লিকেশনকে আরও কার্যকরী এবং প্ল্যাটফর্ম-বান্ধব করতে পারবেন।
Read more